CodeDb Function

Description

You can use the CodeDb function in a module to determine the name of the Database object that refers to the database in which code is currently executing. Use the CodeDb function to access data access objects 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 = CodeDbThe 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 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 executing 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 instance, 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 executed 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 executing. 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 executing.
    Set dbs = CodeDb


    ' Create table-type Recordset.


    Set rst = dbs.OpenRecordSet("Errors", dbOpenTable)
    ' Set index to primary key (ErrorID field).
    rst.Index = "PrimaryKey"
    ' Find error number passed into GetErrorString.


    rst.Seek "=", intError
    ' Return associated error message.


    GetErrorString = rst.Fields!ErrorData.Value


    rst.Close


End Function