使用 OpenCurrentDatabase 方法,將現有的 Microsoft Access 資料庫開啟為目前的資料庫。
語法
表達。OpenCurrentDatabase (filepath, Exclusive, bstrPassword)
expression 代表 Application 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| filepath | 必要 | 字串 | 現有資料庫檔案的名稱,包含路徑名稱及副檔名。 |
| 㥀 | 選用 | 布林值 | 指定是否要以獨佔模式開啟資料庫。 預設值為 False,指定要以分享模式開啟資料庫。 |
| bstr密碼 | 選用 | 字串 | 開啟指定資料庫的密碼。 |
傳回值
無
註解
使用此方法可從另一個應用程式開啟資料庫,該應用程式透過自動化控制 Microsoft Access,先前稱為 OLE 自動化。 例如,您可以使用 Excel 中的 OpenCurrentDatabase 方法Microsoft在 Access 視窗中開啟Northwind.mdb範例資料庫。 從其他應用程式建立 Access 實例之後,您也必須建立新的資料庫,或指定要開啟的特定資料庫。 此資料庫會在 Access 視窗中開啟。
如果您已開啟資料庫,並想要在 Access 視窗中開啟另一個資料庫,您可以使用 CloseCurrentDatabase 方法先關閉第一個資料庫,再開啟另一個資料庫。
注意事項
使用 OpenAccessProject 方法,將現有的 Access 專案 (.adp) 開啟為目前的資料庫。
注意事項
請勿將 OpenCurrentDatabase 方法與 ADO) Open 方法 (ActiveX 資料物件,或 DAO) OpenDatabase 方法 (資料存取物件混淆。 OpenCurrentDatabase 方法會在 Access 視窗中開啟資料庫。 DAO OpenDatabase 方法會傳回 Database 物件變數,該變數代表特定資料庫,但實際上不會在 Access 視窗中開啟該資料庫。
範例
下列範例會透過自動化從另一個應用程式開啟 Access 資料庫,然後在該資料庫中開啟表單。
您可以在任何可作為 COM 元件的應用程式的 Visual Basic 模組中輸入下列程式碼。 例如,您可以從 Excel、Visual Basic 或 Access 執行下列程式碼。
當指向 Application 物件的變數超出範圍時,它所代表的 Access 實例也會關閉。 因此您必須將此變數宣告於模組層級。
' Include the following in Declarations section of module.
Dim appAccess As Access.Application
Sub DisplayForm()
Dim strDB as String
' Initialize string to database path.
Const strConPathToSamples = "C:\Program " _
& "Files\Microsoft Office\Office11\Samples\"
strDB = strConPathToSamples & "Northwind.mdb"
' Create new instance of Microsoft Access.
Set appAccess = _
CreateObject("Access.Application")
' Open database in Microsoft Access window.
appAccess.OpenCurrentDatabase strDB
' Open Orders form.
appAccess.DoCmd.OpenForm "Orders"
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。