Share via


DAO Record Field Exchange: Working with the Wizard Code

OverviewHow Do IFAQSampleODBC Driver List

This article explains the code that the wizards write to support DFX and how you might want to alter that code.

****Note   ****This article is about the DAO version of record field exchange. If you are using the MFC ODBC classes rather than the MFC DAO classes, see the article Record Field Exchange: Working with the Wizard Code instead.

When you create a recordset class with a wizard, the wizard writes the following DFX-related elements for you, based on the data source, table, and column (field) choices you make in the wizard:

  • Declarations of the recordset field data members as described in the article DAO Recordset: Architecture

  • An override of

  • Initialization of recordset field data members in the recordset class constructor

The Field Data Member Declarations for DAO

The wizards write a recordset class declaration in an .H file that resembles the following for a user-defined class called CSectionSet:

class CSectionSet : public CDaoRecordset
{
public:
    CSectionSet(CDaoDatabase* pDatabase = NULL);
    DECLARE_DYNAMIC(CSectionSet)

// Field/Param Data
    //{{AFX_FIELD(CSectionSet, CDaoRecordset)
    CString    m_CourseID;
    CString    m_SectionNo;
    CString    m_InstructorID;
    CString    m_RoomNo;
    CString    m_Schedule;
    int        m_Capacity;
    //}}AFX_FIELD

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CSectionSet)
    public:
    virtual CString GetDefaultDBName();
    virtual CString GetDefaultSQL();
    virtual void DoFieldExchange(CDaoFieldExchange*
                                                  pFX);
    //}}AFX_VIRTUAL

// Implementation
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

};

Notice the following key features about the class above:

  • Special “//{{AFX_FIELD” comments that bracket the field data member declarations. ClassWizard uses these to update your source file.

  • The wizard overrides several virtual functions. The most important of these is the member function.

****Caution   ****Never edit the code inside “//{{AFX” brackets. Always use ClassWizard. If you add parameter data members or new field data members that you bind yourself, add them outside the brackets.

The DoFieldExchange Override for DAO

is the heart of DFX. The framework calls DoFieldExchange any time it needs to move data either from data source to recordset or from recordset to data source. DoFieldExchange also supports obtaining information about field data members through the and member functions.

The following DoFieldExchange override is for a user-defined CSectionSet class. ClassWizard writes the function in the .CPP file for your recordset class.

void CSectionSet::DoFieldExchange(CDaoFieldExchange* pFX)
{
    //{{AFX_FIELD_MAP(CSectionSet)
    pFX->SetFieldType(CDaoFieldExchange::outputColumn);
    DFX_Text(pFX, _T("CourseID"), m_CourseID);
    DFX_Text(pFX, _T("SectionNo"), m_SectionNo);
    DFX_Text(pFX, _T("InstructorID"), m_InstructorID);
    DFX_Text(pFX, _T("RoomNo"), m_RoomNo);
    DFX_Text(pFX, _T("Schedule"), m_Schedule);
    DFX_Short(pFX, _T("Capacity"), m_Capacity);
    //}}AFX_FIELD_MAP
}

Notice the following key features of the function:

  • The special “//{{AFX_FIELD_MAP” comments. ClassWizard uses these to update your source file. This section of the function is called the “field map.”

  • A call to CDaoFieldExchange::SetFieldType, through the pFX pointer. This call specifies that all DFX function calls up to the end of DoFieldExchange or the next call to SetFieldType are “output columns.” See in the Class Library Reference for more information.

  • Several calls to the and global functions — one per field data member. These calls specify the relationship between a column name on the data source and a field data member. The DFX functions do the actual data transfer. The class library supplies DFX functions for all of the common data types. For more information about DFX functions, see the article DAO Record Field Exchange: Using the DFX Functions and, in the Class Library Reference under Macros and Globals, .

  • The pFX pointer to a object that the framework passes when it calls DoFieldExchange. The CDaoFieldExchange object specifies the operation that DoFieldExchange is to perform, the direction of transfer, and other context information.

  • The use of the _T macro for Unicode enabling. For more information, see the article Strings: Unicode and Multibyte Character Set (MBCS) Support.

The Recordset Constructor for DAO

The recordset constructor that the wizards write contains two things related to DFX:

  • An initialization for each field data member

  • An initialization for the data member, which contains the number of field data members

The constructor for the CSectionSet recordset example looks like this:

CSectionSet::CSectionSet(CDaoDatabase* pdb)
    : CDaoRecordset(pdb)
{
    //{{AFX_FIELD_INIT(CSectionSet)
    m_CourseID = _T("");
    m_SectionNo = _T("");
    m_InstructorID = _T("");
    m_RoomNo = _T("");
    m_Schedule = _T("");
    m_Capacity = 0;
    m_nFields = 6;
    //}}AFX_FIELD_INIT
    m_nDefaultType = dbOpenDynaset;

    m_bCheckCacheForDirtyFields = TRUE;
}

This code initializes all of the field data members that require initialization and specifies how many field data members there are (in ). The code also sets the values of two special recordset data members:

  • m_nDefaultType   ****Set to the type of recordset you specify in the wizard. All recordsets created from this class default to the type set here, but you can override the default for any particular recordset object by specifying a new type when you call .

  • m_bCheckCacheForDirtyFields   ****If set to TRUE (the default), the recordset uses a “double-buffering” mechanism to detect edits to fields by comparing them to a copy of the record. For more information, see the article DAO Record Field Exchange: Double Buffering Records.

****Note   ****The code above is enabled for Unicode.

****Important   ****If you add any field data members manually, you must increment m_nFields. Do so with another line of code outside the “//{{AFX_FIELD_INIT” brackets, such as:

    m_nFields += 3;

This is the code for adding three new fields. If you add any parameter data members, you must initialize the data member, which contains the number of parameter data members. Put the m_nParams initialization outside the brackets.

For more information about these special recordset data members, see the article DAO Recordset: Architecture.

See Also   DAO: Where Is..., DAO Record Field Exchange (DFX), DAO Record Field Exchange: Using DFX, DAO Record Field Exchange: How DFX Works, DAO Recordset, DAO Queries: Filtering and Parameterizing Queries