The CurrentDb Function

We have seen that DAO refers to the current database as:

DBEngine.Workspaces(0).Databases(0)

or, through default collections, as:

DBEngine(0)(0)

However, within Microsoft Access, there is a preferred way to refer to this database, since, unlike DBEngine(0)(0), it is always current with respect to changes made using the Access graphical interface. This preferred way is to use the Access function CurrentDb. Unfortunately, there is some confusion as to precisely what this function does.

Here is part of what the Access Help system says about this function:

The CurrentDb function returns an object variable of type Database that represents the database currently open in the Microsoft Access window.

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 Database
          Set dbsA = CurrentDb
          Set dbsB = CurrentDb

This certainly makes it appear as though the object variables dbsA and dbsB point to a single Database object, namely, the currently open database. In other words, executing the instruction:

Set db = CurrentDb ...

Get Access Database Design and Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.