Home | Overview | How Do I | FAQ | Sample | Tutorial | ODBC 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 CDaoDatabase or CDaoRecordset object. Normally you don't need an explicit CDaoWorkspace object, so you let MFC implicitly provide one. For a discussion, see the article DAO Database: Using Workspaces and Databases.
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:
You have a CDaoDatabase object based on the workspace. Access the CDaoDatabase object's m_pWorkspace data member to obtain a CDaoWorkspace 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.
You have a CDaoRecordset object indirectly based on the workspace (through a CDaoDatabase). Follow these steps:
// 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.
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