DAO Record Field Exchange: Double Buffering Records

OverviewHow Do IFAQSampleODBC Driver List

This article explains the double buffering mechanism that MFC uses to detect changes to the current record in a recordset. Topics covered include:

  • Double buffering: definition

  • Using double buffering

  • Effects of double buffering

In the DAO database classes, records are double buffered by default. For information about turning double buffering off, see Using Double Buffering.

Double Buffering: Definition

In MFC's class, double buffering is a mechanism that simplifies detecting when the current record in a recordset has changed. Using double buffering with your DAO recordsets reduces the amount of work you have to do when adding new records and editing existing records.

By default, your MFC DAO recordsets keep a second copy of the edit buffer (the field data members of the recordset class, taken collectively; DAO Help calls the corresponding buffer a copy buffer). As you make changes to the data members, MFC compares them to the copy (the double buffer) to detect the changes.

****Note   ****Not all fields are double buffered by default. Variable length fields, such as those containing binary data, are not. But you can choose to double buffer them if you wish. Note that this can affect performance if the binary data is large.

The alternative to double buffering — not keeping a copy of the data — requires you to make additional function calls when you edit a field of the current record.

For example, suppose your user changes the name of her contact person at company X. With double buffering, MFC detects the change for you. Without it, you must accompany the change with a call to and a call to (with a parameter of FALSE). If a field is supposed to be Null, you must explicitly call SetFieldNull. You must make these calls for every change to a record field.

In general, you get better performance with double buffering off, but double buffering is a considerable convenience when performance is not critical.

Using Double Buffering

Double buffering is the default for recordset fields of most data types, but not for the variable-length data types, such as text and binary. Because data of those types is potentially very large, storing a second copy of the data is not a good default. However, if you know your data will not be prohibitively large, you can turn double buffering on for these types as well. You can also choose to turn double buffering off. You can control double buffering for the whole recordset or on a field-by-field basis.

Overall double buffering is controlled by the data member. Field-by-field double buffering is controlled by the dwBindOptions parameter to any of the DFX functions (, , , and so on).

To turn double buffering on or off for the whole recordset

  • Set the value of to AFX_DAO_ENABLE_FIELD_CACHE (on) or AFX_DAO_DISABLE_FIELD_CACHE (off). A typical place to do this is in the recordset constructor.

****Note   ****If this data member is TRUE (the default), double buffering is on for all field data members except those of variable-length data type (binary, long binary, and text). If this data member is FALSE, double buffering is off for all fields, regardless of data type.

To turn double buffering on or off for a specific field in the recordset

  • In the DFX function call for the field, set the dwBindOptions parameter to TRUE (on) or FALSE (off).

DFX function calls are made in your recordset class's member function. See the article DAO Record Field Exchange: Working with the Wizard Code for a discussion of DoFieldExchange.

Possible values for dwBindOptions are:

  • AFX_DAO_ENABLE_FIELD_CACHE   (Default) Double buffering is on for the field.

  • AFX_DAO_DISABLE_FIELD_CACHE   Double buffering is off for the field.

In the following example, double buffering is on for the first field but explicitly turned off for the second field.

void CSections::DoFieldExchange(CDaoFieldExchange* pFX)
{
    //{{AFX_FIELD_MAP(CSections)
    pFX->SetFieldType(CDaoFieldExchange::outputColumn);
    DFX_Short(pFX, "Capacity", m_Capacity);
    DFX_Short(pFX, "Enrollment", m_Enrollment,
                         AFX_DAO_DISABLE_FIELD_CACHE);
    //}}AFX_FIELD_MAP
}

Effects of Double Buffering

If double buffering is on, as it is by default, record data is double buffered when:

  • You call to edit the fields of the current record.

  • You call to add a new record to the recordset.

MFC copies the field data members of the recordset into a buffer (the double buffer). Then it uses the copy to detect changes to the original field data members in the recordset. For more discussion of how double buffering fits into the record updating process, see the article DAO Record Field Exchange: How DFX Works.

****Tip   ****To improve performance you might sometimes prefer to turn double buffering off. However, alternatives include:

  • Creating queries that only return the fields and rows that you actually need.

  • Specifying in your recordset only the fields that you will always use. Then you can supplement those fields by calling at appropriate times to retrieve the fields you need only occasionally. See the article DAO Recordset: Binding Records Dynamically.

In the Class Library Reference, see .

See Also   DAO: Where Is..., DAO Recordset, DAO Record Field Exchange (DFX), DAO Record Field Exchange: Working with the Wizard Code