ClassWizard: Mapping Form Controls to Recordset Fields

OverviewHow Do I

A database form based on class (for the MFC ODBC classes) or (for the MFC DAO classes) uses dialog data exchange (DDX) to exchange data between the form's controls and the field data members of the form's associated recordset object. This article explains how to use ClassWizard to set up the DDX connection between the controls and the recordset.

This connection is different from the normal use of DDX, which connects the controls in a dialog box directly to the data members of the associated dialog class. DDX for a record view object is indirect. The connection goes from form controls through the record view object to the field data members of the associated recordset object. For more information, see the articles Record Views and ClassWizard: Foreign Objects. The following table summarizes the mapping process.

Mapping Record View Controls to a Recordset

To Do this
Specify the form control Select the record view class on ClassWizard's Member Variables tab and select one of the form's control IDs
Specify the recordset data member to connect it to Use the Add Variable dialog box to select a recordset data member indirectly

The following procedure assumes you have already followed the procedures in the article ClassWizard: Creating a Database Form. Specifically, you've created a -derived or -derived class and added some field data members to it, created a dialog-template resource, created a -derived or -derived class, and associated the record view class with the recordset class. Your record view class is associated with the dialog-template resource, to which you've added controls with the Visual C++ dialog editor.

To map the form controls to the recordset

  1. Click ClassWizard's Member Variables tab.

  2. In the Class Name box, select the name of your record view class.

  3. In the Control IDs box, select a control ID.

  4. Click the Add Variable button to name a variable associated with the control.

  5. In the Add Member Variable dialog box, choose a variable name by selecting a recordset data member in the Member Variable Name drop-down list box.

    Important   Use the drop-down list box to select a data member name from the associated recordset (a foreign object). The names that appear are of the form m_pSet->m_recordsetVarName. You can type the name instead, but selecting from the drop-down list is faster and more accurate.

    All variables of the recordset class appear in the box, not just variables of the currently selected data type.

  6. Click OK to close the Add Member Variable dialog box.

  7. Repeat steps 3 through 6 for each control in the record view that you want to map to a recordset field data member.

  8. Click OK to close ClassWizard.

By selecting a name from the drop-down list, you specify a connection that passes through the record view object to a field data member of its associated recordset object.

Tip   If you're running ClassWizard with the Visual C++ dialog editor open, the following shortcut is available. Choose a control on the form; then press CTRL and double-click the mouse. This opens ClassWizard's Add Member Variables dialog box, where you can bind a recordset field data member to the control. (If you use this shortcut before a class has been created for the dialog template, ClassWizard opens and displays its Add Class dialog box.)

If you follow a simple rule when placing controls on your record view form, ClassWizard is able to pre-select the most likely recordset member in the dialog box. The rule is to place the static text label for the control ahead of the corresponding control in the tab order.

Tip   You can use CTRL+double-click for pushbuttons too. In these cases, ClassWizard creates a message handler function for the BN_CLICKED notification message in your record view class. You can edit its code to specify the button's action.

See Also   ClassWizard: Foreign Objects