Using Default Collections to Write Shorter Code

Almost all of the DAO objects have default collections. With these default collections, you can use a shorter reference to objects in code. By using the full syntax, you can return a reference to a TableDef object and a Field object, as shown in the following code. In this example, strDbPath is the path to the database, strTableName is the name of the table, and strFieldName is the name of the field:

Dim dbs As Database, tdf As TableDef

Set dbs = OpenDatabase(strDbPath)
Set tdf = dbs.TableDefs(strTableName)
Set fld = tdf.Fields(strFieldName)

Because the TableDefs collection is the default collection of the Database object, and the Fields collection is the default collection of the TableDef object, you can shorten this code to:

Dim dbs As Database, tdf As TableDef

Set dbs = OpenDatabase(strDbPath)
Set tdf = dbs(strTableName)
Set fld = tdf(strFieldName)

Note Although using default collections produces shorter code that may be more efficient, it also reduces the readability of the code. It’s always advisable to use comments in your code to provide information about what your code is doing.

The following table lists the default collections available in DAO.

Object Default collection
DBEngine Workspaces
Workspace Databases
Database TableDefs
Container Documents
QueryDef Parameters
Recordset Fields
Relation Fields
TableDef Fields
Index Fields
Group Users
User Groups