The CurrentDb function returns an object variable of type Database that represents the database currently open in the Microsoft Access window.
CurrentDb
In order to manipulate the structure of your database and its data from Visual Basic, you must use data access objects. The CurrentDb function provides a way to access the current database from Visual Basic code without having to know the name of the database. Once you have a variable that points to the current database, you can also access and manipulate other objects and collections in the data access object hierarchy.
You can use the CurrentDb function to create multiple object variables that refer to the current database. In the following example, the variables dbsA and dbsB both refer to the current database.
Dim dbsA As Database, dbsB As DatabasedbsA = CurrentDbdbsB = CurrentDb
Note 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. In Microsoft Access for Windows 95, you should use the CurrentDb function instead. The CurrentDb function creates another instance of the current database, while the DBEngine(0)(0) syntax refers to the open copy of the current database. Using the CurrentDb function enables you to create more than one variable of type Database that refer to the current database. 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.
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. 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. The following example returns a pointer to the current database and to a database called Contacts.mdb.
Dim dbsCurrent As Database, dbsContacts As DatabasedbsCurrent = CurrentDbdbsContacts = DBEngine.Workspaces(0).OpenDatabase("Contacts.mdb")
The following example uses the CurrentDb function to return a Database object variable pointing to the current database. It then enumerates all the fields in the Employees table in that database.
Sub ListFields() Dim dbs As Database, tdf As TableDef, fld As Field ' Return Database object variable pointing to current database. Set dbs = CurrentDb ' Return TableDef object variable pointing to Employees table. Set tdf = dbs.TableDefs!Employees ' Enumerate fields in Employees table. For Each fld In tdf.Fields Debug.Print fld.Name Next fldSub