Application.CodeDb method (Access)

Use the CodeDb method in a code module to determine the name of the Database object that refers to the database in which code is currently running. Use the CodeDb method to access Data Access Objects (DAO) that are part of a library database.

Syntax

expression.CodeDb

expression A variable that represents an Application object.

Return value

Database

Remarks

For example, you can use the CodeDb method in a module in a library database to create a Database object referring to the library database. You can then open a recordset based on a table in the library database.

Set database= CodeDb

The CodeDb method returns a Database object for which the Name property is the full path and name of the database from which it is called. This method can be useful when you need to manipulate the Data Access Objects in your library database.

When you call a method in a library database, the database from which you have called the method remains the current database, even while code is running in a module in the library database. To refer to the Data Access Objects in the library database, you need to know the name of the Database object that represents the library database.

For example, suppose you have a table in a library database that lists error messages. To manipulate data in the table from code, you could use the CodeDb method to determine the name of the Database object that refers to the library database that contains the table.

If the CodeDb method is run from the current database, it returns the name of the current database, which is the same value returned by the CurrentDb method.

Example

The following example uses the CodeDb method to return a Database object that refers to a library database. The library database contains both a table named Errors and the code that is currently running. After the CodeDb method determines this information, the GetErrorString function opens a table-type recordset based on the Errors table. It then extracts an error message from a field named ErrorData based on the Integer value passed to the function.

Function GetErrorString(ByVal intError As Integer) As String 
 Dim dbs As Database, rst As RecordSet 
 
 ' Variable refers to database where code is running. 
 Set dbs = CodeDb 
 ' Create table-type Recordset object. 
 Set rst = dbs.OpenRecordSet("Errors", dbOpenTable) 
 ' Set index to primary key (ErrorID field). 
 rst.Index = "PrimaryKey" 
 ' Find error number passed to GetErrorString function. 
 rst.Seek "=", intError 
 ' Return associated error message. 
 GetErrorString = rst.Fields!ErrorData.Value 
 rst.Close 
End Function

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.