CurrentDb 方法 (访问)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.



expression:表示 Application 对象的变量。expression A variable that represents an Application object.

返回值Return value




在 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 窗口中打开当前数据库的同时运行另一个数据库, 请使用Workspace对象的OpenDatabase方法。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.