ClassWizard: Binding Recordset Fields to Table Columns

OverviewHow Do I

Both AppWizard and ClassWizard bind all columns of your selected tables to the recordset. This article applies to both the MFC ODBC classes and the MFC DAO classes. The article explains how to:

  • Remove data members for any columns you don't want in the recordset.

    If you want only a subset of the columns bound by the wizards, use ClassWizard to remove the field data members for any columns you don't want.

  • Select table columns and map them to recordset field data members.

    If you subsequently remove any of the bindings the wizards made, you can later rebind them with ClassWizard.

  • Update columns in your recordset to reflect new columns in the table on the data source.

This and related articles use the terms "column" and "field" interchangeably when referring to recordset fields.

Removing Columns from Your Recordset

You might sometimes need to remove columns from a recordset. Both AppWizard and ClassWizard automatically bind all table columns to the recordset. If you want only a subset of the columns, use the following procedure.

To remove a column from a recordset

  1. In ClassWizard, click the Member Variables tab.

  2. Click a member variable name in the Members column of the Column Names box.

  3. Click Delete Variable.

    The member variable is removed from your recordset class. (The column in the data source to which the member variable was bound is not deleted.)

Warning   Be careful not to remove any columns that are part of the table's primary key. In some tables, the primary key is a single column; in others, it's composed of two or more columns taken together. Removing these columns could damage your data on the data source.

Adding Columns to Your Recordset

Once you select a table, ClassWizard displays a list of all columns in the table. You can remove column bindings, and you can later rebind columns whose bindings have been removed. The following procedure explains how to bind an unbound column.

To add a column to the recordset

  1. Create the recordset class and associate it with one or more database tables.

    If you create the class with ClassWizard, follow the procedure To Create the Recordset Class in the article ClassWizard: Creating a Recordset Class.

    If you create the class with AppWizard, follow the information in the article Database Options in AppWizard.

  2. In ClassWizard, on the Member Variables tab, select the recordset class name in the Class Name box if it is not already selected.

  3. Select a column in the Column Names box.

  4. Click Add Variable to open the Add Member Variable dialog box.

  5. Type a name for the recordset data member that will represent the selected column.

  6. ClassWizard supplies a standard data member prefix, m_, for the name. Append the rest of the name to this prefix or type over it.

    The variable's property and data type are already specified in the dialog box. The property for these variables is always "Value." The data type is based on the data type of the column on the data source.

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

  8. Click OK to close ClassWizard.

    ClassWizard writes your class files to the specified directory and adds them to your project.

Tip   Rather than binding columns to data members one at a time, you can click the Bind All button to bind all columns. ClassWizard gives the data members default names based on the column names. Binding columns one at a time gives you more control over the columns bound and how they're named, but Bind All is quick and easy.

The following figure shows the Member Variables tab with a data member added for one of the available columns.

Adding a Data Member to the Recordset

Updating Your Recordset's Columns

Tables in a data source sometimes change—in particular, columns may be added to a table. To bring your recordset class up to date with respect to these changes, use the Update Columns button.

To update the columns in your recordset

  1. On ClassWizard's Member Variables tab, click your recordset class.

  2. Click Update Columns.

Any new columns are shown in the list but not yet bound to recordset field data members. To bind them, see the procedure To add a column to the recordset in Adding Columns to Your Recordset.

When you use Update Columns, columns you've previously bound to recordset data members are left alone; to unbind a column, delete its data member. Any columns in the table that aren't bound to the recordset disappear from the refreshed recordset.

See Also   ClassWizard: Creating a Database Form, ClassWizard: Creating a Recordset Class