Application.CurrentDb 方法 (Access)Application.CurrentDb method (Access)

CurrentDb方法會傳回在 Microsoft Access 視窗中開啟目前代表資料庫的資料庫類型的物件變數。The CurrentDb method returns an object variable of type Database that represents the database currently open in the Microsoft Access window.

語法Syntax

運算式CurrentDbexpression.CurrentDb

expression 代表 Application 物件的變數。expression A variable that represents an Application object.

傳回值Return value

DatabaseDatabase

註解Remarks

注意

在 Microsoft Access 中, CurrentDb方法會在 Microsoft Access 資料庫中建立 Microsoft Office 12.0 Access Connectivity Engine 物件程式庫的隱藏的參照。In Microsoft Access the CurrentDb method establishes a hidden reference to the Microsoft Office 12.0 Access Connectivity Engine object library in a Microsoft Access database.

若要修改的結構,您的資料庫以及其資料從 Visual Basic,您必須使用資料存取物件 (DAO)。To manipulate the structure of your database and its data from Visual Basic, you must use Data Access Objects (DAO). CurrentDb 方法可讓您從 Visual Basic 程式碼存取目前的資料庫,而不用知道資料庫的名稱。The CurrentDb method provides a way to access the current database from Visual Basic code without having to know the name of the database. 您有一個變數,指向 [目前的資料庫之後,您也可以存取,並操作其他物件和 DAO 階層中的集合。After you have a variable that points to the current database, you can also access and manipulate other objects and collections in the DAO hierarchy.

CurrentDb 方法可用來建立參照目前資料庫的多個物件變數。You can use the CurrentDb method to create multiple object variables that refer to the current database. 在下列範例中,變數dbsAdbsB同時參照至目前的資料庫:In the following example, the variables dbsA and dbsB both refer to the current database:

Dim dbsA As Database, dbsB As Database 
Set dbsA = CurrentDb 
Set dbsB = CurrentDb

注意

在舊版的 Microsoft Access 中,您可能已經使用語法DBEngine.Workspaces(0).Databases(0)DBEngine(0)(0)指標回到目前的資料庫。In previous versions of Microsoft Access, you may have used the syntax DBEngine.Workspaces(0).Databases(0)or DBEngine(0)(0)to return a pointer to the current database. 在 Microsoft Access 2000 及更新版本中,您應該改為使用CurrentDb方法。In Microsoft Access 2000 and later, you should use the CurrentDb method instead. CurrentDb方法會建立目前的資料庫,另一個執行個體時DBEngine(0)(0)語法指的是開啟的目前的資料庫副本。The CurrentDb method creates another instance of the current database, while the DBEngine(0)(0)syntax refers to the open copy of the current database. CurrentDb 方法能讓您為參照目前資料庫的 Database 類型建立一個以上的變數。The CurrentDb method enables you to create more than one variable of type Database that refers to the current database. Microsoft Access 仍支援DBEngine(0)(0)語法中,但您應該考慮將此修改您的程式碼以避免可能發生在多使用者資料庫中的衝突。Microsoft Access still supports the DBEngine(0)(0)syntax, but you should consider making this modification to your code in order to avoid possible conflicts in a multiuser database.

如果您需要使用另一個資料庫,Microsoft Access 視窗中目前的資料庫已開啟的同時,使用OpenDatabase方法的Workspace物件。If you need to work with another database at the same time that the current database is open in the Microsoft Access window, use the OpenDatabase method of a Workspace object. OpenDatabase 方法不會在 Microsoft Access 視窗中實際開啟第二個資料庫,它只是傳回代表第二個資料庫的 Database 變數。The OpenDatabase method doesn't actually open the second database in the Microsoft Access window; it simply returns a Database variable representing the second database. 下列範例讓指標返回目前資料庫,並返回名為 Contacts.mdb 的資料庫:The following example returns a pointer to the current database and to a database called Contacts.mdb:

Dim dbsCurrent As Database, dbsContacts As Database 
Set dbsCurrent = CurrentDb 
Set dbsContacts = DBEngine.Workspaces(0).OpenDatabase("Contacts.mdb")

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.