Application.CloseCurrentDatabase 方法 (Access)

使用 CloseCurrentDatabase 方法,從透過自動化開啟資料庫的另一個應用程式,關閉目前的資料庫、Microsoft Access 資料庫或 Access 專案 (.adp) 。

語法

運算式CloseCurrentDatabase

expression 代表 Application 物件的變數。

傳回值

註解

例如,您可能會在 Microsoft Excel 中使用此方法,於開啟另一個資料庫之前將目前已在 Microsoft Access 視窗中開啟的資料庫關閉。

當您已透過自動化執行而經由另一個應用程式來開啟 Microsoft Access 資料庫時,CloseCurrentDatabase 方法特別有用。 從另一個應用程式建立 Microsoft Access 實例之後,您也必須建立新的資料庫,或指定要開啟的現有資料庫。 此資料庫會在 Microsoft Access 視窗下開啟。

如果您使用 CloseCurrentDatabase 方法來關閉 Microsoft Access 現有實例中已開啟的資料庫,之後您就可以開啟不同的資料庫而不需另外建立一個 Microsoft Access 實例。

範例

下列範例會透過自動化從另一個應用程式開啟 Microsoft Access 資料庫、建立新表單並加以儲存,然後關閉資料庫。

您可以在任何可作為 COM 元件的應用程式的 Visual Basic 模組中輸入下列程式碼。 例如,您可以從 Microsoft Excel 或 Microsoft Visual Basic 來執行下列程式碼。

當指向 Application 物件的變數超出範圍時,變數所代表的 Microsoft Access 實例也會關閉。 因此您必須將此變數宣告於模組層級。

' Enter the following in the Declarations section of the module. 
Dim appAccess As Access.Application 

Sub CreateForm() 
 Const strConPathToSamples = "C:\Program Files\Microsoft Office\Office12\Samples\" 
 
 Dim frm As Form, strDB As String 
 
 ' Initialize string to database path. 
 strDB = strConPathToSamples & "Northwind.mdb" 
 ' Create new instance of Microsoft Access. 
 Set appAccess = CreateObject("Access.Application") 
 ' Open database in Microsoft Access window. 
 appAccess.OpenCurrentDatabase strDB 
 ' Create new form. 
 Set frm = appAccess.CreateForm 
 ' Save new form. 
 appAccess.DoCmd.Save , "NewForm1" 
 ' Close currently open database. 
 appAccess.CloseCurrentDatabase 
 Set AppAccess = Nothing 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應