Share via


DAO: Accessing Implicit MFC DAO Objects

OverviewHow Do IFAQSampleODBC Driver List

This article describes how to access the implicit MFC DAO objects that MFC creates for you in certain situations. The classic example is the workspace object associated with an existing or object. Normally you don't need an explicit object, so you let MFC implicitly provide one. For a discussion, see the article DAO Database: Using Workspaces and Databases.

The Most Likely Case

In the most likely case — that you already have a CDaoDatabase or a CDaoRecordset object associated with the workspace you want to access — you can use data members of these objects to obtain a pointer to the implicit CDaoWorkspace object that they belong to. There are two scenarios, based on whether you are working from:

  • A database object (Scenario 1)

  • A recordset object (Scenario 2)

Scenario 1. A Database Object: One Level of Indirection

You have a object based on the workspace. Access the CDaoDatabase object's data member to obtain a pointer, like this:

// pdbAccounts is a pointer to a CDaoDatabase object
// for the Accounts database
CDaoWorkspace* pws = pdbAccounts->m_pWorkspace;

Or you might simply use the implicit workspace to call a CDaoWorkspace member function:

pdbAccounts->m_pWorkspace->BeginTrans( );

Calling transaction functions in this way is common.

Scenario 2. A Recordset Object: Two Levels of Indirection

You have a object indirectly based on the workspace (through a ). Follow these steps:

  1. Access the CDaoRecordset object's data member to obtain a CDaoDatabase pointer.

  2. Access the database object's data member to obtain a pointer, like this:

    // rsDelinquentAccts is an existing CDaoRecordset
    // object based on the Accounts database
    CDaoDatabase* pdbAccounts = rs.m_pDatabase;
    CDaoWorkspace* pws = pdbAccounts->m_pWorkspace;
    

Or you might simply use the implicit workspace behind your recordset's implicit database to call a CDaoWorkspace member function:

pdbAccounts->m_pWorkspace->CommitTrans( );

****Note   ****This is the recommended method for accessing such functions because it doesn’t create a copy of a pointer to an implicit object. Copies of such pointers can be dangerous.

Uses for the Workspace Pointer

You can use the workspace pointer obtained in this indirect way to access the Workspaces collection, the Databases collection, properties of the database engine, and so on. Note that in most cases the workspace accessed this way is DAO's default workspace.

****Caution   ****If you store a copy to one of these pointers, be careful not to use it after the original object goes out of scope or is otherwise destroyed.

See Also   DAO: Where Is..., DAO: Creating, Opening, and Closing DAO Objects