Description
You can use the CodeDb function 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 function to access Data Access Objects (DAO) that are part of a library database.
For example, you can use the CodeDb function 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. Syntax Set database = CodeDb The CodeDb function has the following argument.Argument | Description |
database | A Database object variable. |
Remarks The CodeDb function returns a Database object for which the Name property is the full path and name of the database from which it is called. This function can be useful when you need to manipulate the Data Access Objects in your library database.
When you call a function in a library database, the database from which you have called the function remains the current database, even while code is running in a module in the library database. In order 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 function to determine the name of the Database object that refers to the library database that contains the table. If the CodeDb function is run from the current database, it returns the name of the current database, which is the same value returned by the CurrentDb function.See Also CurrentDb function.
Example The following example uses the CodeDb function 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 function 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