DAO and MFC

OverviewHow Do IFAQSampleODBC Driver List

This article describes MFC’s implementation of Microsoft Data Access Objects (DAO). Topics covered include:

  • How MFC Encapsulates DAO

  • Mapping of DAO objects to MFC classes

  • Key differences between MFC and DAO

  • Further reading about the MFC DAO classes

****Note   ****Whether you use the MFC DAO classes or the MFC ODBC classes depends on your situation and your needs. For a discussion of the differences between the two and guidance on choosing one, see the article Database Topics (DAO).

****Caution   ****DAO is not supported on Win32s. Further, DAO 3.x is not thread-safe. You should use DAO only in the primary thread of an application.

How MFC Encapsulates DAO

The MFC DAO classes treat DAO much as the MFC classes for programming Windows treat the Windows API: MFC encapsulates, or "wraps," DAO functionality in a number of classes that correspond closely to DAO objects. Class encapsulates the DAO workspace object, class encapsulates the DAO recordset object, class encapsulates the DAO database object, and so on.

MFC's encapsulation of DAO is thorough, but it is not completely one-for-one. Most major DAO objects do correspond to an MFC class, and the classes supply generally thorough access to the underlying DAO object's properties and methods. But some DAO objects, including fields, indexes, parameters, and relations, do not. Instead, the appropriate MFC class provides an interface, via member functions, through which you can access, for example:

  • The fields of a recordset object

  • The indexes or fields of a table

  • The parameters of a querydef

  • The relations defined between tables in a database

Mapping of DAO Objects to MFC Classes

The following tables show how DAO objects correspond to MFC objects. MFC Classes and Corresponding DAO Objects shows the MFC classes and the DAO objects they encapsulate. How MFC Manages DAO Objects Not Mapped to Classes shows how MFC deals with DAO objects that do not map directly to an MFC class.

MFC Classes and Corresponding DAO Objects

Class DAO object Remarks
Workspace Manages a transaction space and provides access to the database engine.
Database Represents a connection to a database.
Tabledef Used to examine and manipulate the structure of a table.
Querydef Used to store queries in a database. You can create recordsets from a querydef or use it to execute action or SQL pass-through queries.
Recordset Used to manage a result set, a set of records based on a table or selected by a query.
Error MFC responds to all DAO errors by throwing exceptions of this type.
None Manages exchange of data between a record in the database and the field data members of a recordset.

How MFC Manages DAO Objects Not Mapped to Classes

DAO object How MFC manages it
Field Objects of classes and encapsulate fields and supply member functions for adding them, deleting them, and examining them.
Index Objects of classes and encapsulate indexes and supply member functions for managing them. Tabledefs can add, delete, and examine indexes. Tabledefs and recordsets can set or get the currently active index.
Parameter Objects of class encapsulate parameters and supply member functions for adding them, deleting them, examining them, and getting and setting their values.
Relation Objects of class encapsulate relations and supply member functions for adding them, deleting them, and examining them.

DAO Objects Not Exposed in MFC

MFC and DAO do not supply abstractions for some objects used within Microsoft Access: Application, Container, Control, Debug, Document, Form, Module, Report, Screen, and Section. If you create a Microsoft Access database and manipulate it from an MFC application, you can’t access those objects through code.

MFC doesn’t supply classes or interfaces to the DAO group and user objects ? to work with DAO security, you must write your own code.

MFC also doesn’t encapsulate DAO property objects, except that the MFC DAO classes do give you access to the properties of all exposed objects.

MFC does give you access to DAO’s DBEngine object, through class .

Accessing the Unexposed DAO Objects

The unexposed objects listed above can  be accessed in two ways:

  • Outside the MFC classes by using the non-MFC C++ classes provided in the DAO SDK, located in the \DAOSDK directory on the Visual C++ CD.

  • Inside the MFC classes by calling DAO directly through a DAO interface pointer supplied by one of the MFC classes. For information, see .

Key Differences Between MFC and DAO

MFC's version of data access objects differs from the underlying structure of DAO in some ways.

How MFC Accesses the Database Engine

DAO has a DBEngine object that represents the Microsoft Jet database engine. The DBEngine object provides properties and methods you can use to configure the database engine.

In MFC, there is no DBEngine object. Access to important properties of the database engine is supplied via class . To set or get these properties, call any of the static member functions of CDaoWorkspace. For more information, see the articles DAO Workspace: The Database Engine and DAO Workspace: Accessing Properties of the Database Engine.

MFC Flattening of the DAO Object Hierarchy

Because MFC doesn't supply a class for every DAO object, the effect is that the DAO object hierarchy is somewhat "flattened" in MFC. The main examples of this flattening are:

  • Putting access to the database engine in class rather than in a database engine class.

  • Encapsulating DAO field, index, parameter, and relation objects inside the classes that represent their owning objects. For example, access to fields is encapsulated in classes and . For information, see the table How MFC Manages DAO Objects Not Mapped to Classes.

MFC and DAO Security

MFC does not encapsulate the DAO user and group objects in any way, which means that MFC doesn't provide DAO's security functionality.

You can still use DAO security from your MFC applications, but you will have to call DAO directly, using the data member of class . That member is a pointer to an interface that gives access to a DAO workspace object's methods and properties. For information about calling DAO directly, see .

****Tip   ****The DAO Software Development Kit (SDK) supplies its own set of C++ classes (not compatible with MFC) for working with DAO. You can use these classes, if you wish, by installing the DAO SDK using \DAOSDK\Disk1\Setup.exe on the Visual C++ CD. These classes provide an additional source of examples for using DAO from C++.

MFC does allow password protection via various MFC classes. For example, when you a CDaoWorkspace object, you can specify a password to protect the database(s) that the workspace contains. To use this functionality, a SYSTEM.MDW file must be available to the database engine on the machine running your application. If no SYSTEM.MDW file is available to the database engine, your application cannot use any of the security features. For information about the SYSTEM.MDW file, see the topic "Permissions Property" in DAO Help.

Further Reading About the MFC DAO Classes

To learn more about using the MFC DAO classes, see the following articles (in the order listed here):

****Tip   ****From any of the MFC help topics in this documentation set, you can get to a topic called DAO: Where Is..., which helps you navigate online to the topics that you need. The topic is always available via the See Also button in the topic window.

See Also   DAO: Where Is...