Share via


ClassWizard: Creating a Database Form

OverviewHow Do I

The Microsoft Foundation Class Library database classes supply class (for ODBC) and class (for DAO) for implementing database forms with controls in which to display record fields. This article explains how to create a record view class with ClassWizard and associate it with a recordset class.

Important   To use the MFC database classes, you must have specified at least minimum database support ("Only include header files") in AppWizard. If you didn't, open file Stdafx.h and add an #include directive for Afxdb.h (if you're using the MFC ODBC classes) or Afxdao.h (if you're using the MFC DAO classes).

The purpose of a record view class is to provide a form view whose controls are mapped directly to the field data members of a recordset object (and indirectly to the corresponding columns in a table on the data source). Setting up this mapping enables dialog data exchange (DDX) directly between the form's controls and the recordset's field data members. This article explains how to make the association.

Tip   The easiest way to use a record view as your application's main view is to do so when you initially run AppWizard. See Database Options in AppWizard.

The following procedure begins the process.

To create a record view associated with a recordset

  1. Create the recordset class.

    See the article ClassWizard: Creating a Recordset Class.

  2. Create a dialog-template resource.

    See Using the Dialog Editor to Add Controls or Dialog Editor Topics for Visual C++ for more information.

    In the Styles and More Styles property pages of your dialog template, set the following properties, as you would for a CFormView object:

    • In the Style box, select Child (WS_CHILD on).

    • In the Border box, select None (WS_BORDER off).

    • Clear the Visible check box (WS_VISIBLE off).

    • Clear the Titlebar check box (WS_CAPTION off).

Tip   As you place controls on your dialog template, you can help ClassWizard be smarter. See the tips in the article ClassWizard: Mapping Form Controls to Recordset Fields.

To continue from step 2, see the following procedure.

To create the record view class

  1. Run ClassWizard with the Visual C++ dialog editor open on your dialog-template resource.

  2. In the Adding a Class dialog box, click Create a new class (unless you are importing or using an existing class). For more information, see Adding a Class.

  3. In the New Class dialog box, specify (for ODBC) or (for DAO) as the base class for the new class.

  4. Enter a name for the class and filenames for its .h and .cpp files and select any other options you need, such as Automation.

  5. Click Create.

  6. In the Select a Recordset dialog box, select the recordset class you created in step 1 of the procedure To create a record view associated with a recordset.

    -or-

    Click New to create a new recordset class. In this case, select a data source and table for the recordset as prompted. Close the Select a Recordset dialog box. On returning to the Member Variables tab, select your new recordset class.

  7. Bind columns to recordset field data members. See the article ClassWizard: Binding Recordset Fields to Table Columns.

    Note   If you next click the Class Info tab, you'll see your recordset class listed in the Foreign Class box. For more information about "foreign" classes, see the article ClassWizard: Foreign Objects.

  8. Click OK to exit ClassWizard.

For information about binding your record view controls to the recordset, see the article ClassWizard: Mapping Form Controls to Recordset Fields.

See Also   ClassWizard: Database Support, ClassWizard: Creating a Recordset Class, ClassWizard: Binding Recordset Fields to Table Columns