The Databases Collection
Using DAO, you can have more than one database open in Access at any time. If you're using an .accdb or .mdb database file, you already have one database open (called the current database). Using the Workspace object's OpenDatabase method, as shown earlier in the example in the "Using Transactions" section, you can open more than one database, and operate on them under the same workspace context. Indeed, if you were to define more than one Workspace object, you could have several databases open, each operating under a different workspace context. The Databases collection contains and manages all databases currently open in the workspace.
The Default (Access) Database
Unless you're working with an Access Data Project, when you create a database in Access, it is automatically added to the Databases collection.
Among its properties and methods, the Database object contains five collections: TableDefs, Containers, QueryDefs, Recordsets, and Relations. Each of these collections and their respective objects and properties are discussed in later sections. In most cases, you will be working with the default Microsoft Access database, which you can refer to using any of the following syntaxes:
DBEngine.Workspaces("#Default Workspace#").Databases(0) DBEngine.Workspaces(0).Databases(0) DBEngine(0).Databases(0) DBEngine(0)(0) CurrentDb()
The current user's default database is an object that you will use quite a lot. Although you can work with it using any of the reference methods listed, in most cases it is often more convenient to assign it to an object variable.
Dim dbs As DAO.Database Set dbs = DBEngine(0)(0)
But far and away the most common method is to use the CurrentDb() function, described in the following section.
In this tutorial:
- Using DAO to Access Data
- Data Access Objects
- New Features in DAO
- Referring to DAO Objects
- The DBEngine Object
- Using Transactions
- The Errors Collection
- The Databases Collection
- The CurrentDb() Function
- Opening an External Database
- Closing and Destroying Database Object References
- DAO Property Types
- Setting and Retrieving Built-In Object Properties
- Setting and Retrieving SummaryInfo Properties
- Creating Schema Objects with DAO
- Creating Indexes
- Creating Relations
- Creating Multi-Value Lookup Fields
- Database Encryption with DAO
- Setting Encryption Options
- Managing Access (JET) Security with DAO
- Creating Security Objects
- Creating and Deleting Groups
- Managing Passwords
- Data Access with DAO
- Modifying a QueryDef
- Filtering and Ordering Recordsets
- Navigating Recordsets
- BOF, EOF
- Navigating Recordsets with Multi-Value Lookup Fields
- Bookmarks and Recordset Clones
- Finding Records
- Working with Recordsets
- Using Arrays with Recordsets
- Working with Attachment Fields
- Append Only Fields