DAO Record Field Exchange: Working with the Wizard Code

HomeOverviewHow Do IFAQSampleTutorialODBC 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:

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:

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

DoFieldExchange 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 IsFieldDirty and IsFieldNull 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 Recordset Constructor for DAO

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

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 m_nFields). The code also sets the values of two special recordset data members:

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 m_nParams 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