DAO Recordset: Creating Recordsets

HomeOverviewHow Do IFAQSampleTutorialODBC Driver List

This article explains the process of creating recordset objects based either on class CDaoRecordset itself or on a class derived from CDaoRecordset.

You can use recordset objects in two ways:

In either case, you can base your recordset objects on a query defined by a CDaoQueryDef object or a CDaoTableDef object, or you can specify the recordset objects’ SQL strings either at design time, when you create the class with a wizard, or at run time, when you pass a string containing an SQL statement to the CDaoRecordset::Open member function.

The following table describes the characteristics of a recordset, depending on how you create it:

If you base your recordset on... It has these characteristics...
A querydef The recordset inherits the querydef’s SQL string.
A tabledef The recordset (a table-type recordset) is based on the table. Creating a recordset from a tabledef is similar to creating one from a querydef. See the following procedure for creating from a querydef.
The SQL string specified at design time, in the wizard The recordset uses the SQL string retrieved by calling its GetDefaultSQL member function. The wizard codes the string as the value returned by this function.
An SQL string specified at run time, in the Open call The SQL string passed to Open overrides the SQL defined at design time.

The following procedure shows how to use a querydef as the basis for a recordset. Using a tabledef to create a recordset is similar.

To create a recordset from a querydef

  1. Create the querydef, as described in Creating a Query with a Querydef, or use an existing querydef object.

  2. Construct a CDaoRecordset object.

  3. Call the recordset object's Open member function, passing a pointer to the querydef object.

Opening the recordset runs the query, using the SQL statement defined by the querydef.

Note   You can only create a dynaset-type or snapshot-type recordset from a querydef.

The following code shows the process of creating a recordset from a querydef:

// pdb is a pointer to an open CDaoDatabase object
try
{
    // Construct the querydef
    CDaoQueryDef qd( pdb );

    // Set up the SQL string and create the querydef
    CString strSQL = 
      _T(“SELECT [Company Name] FROM Publishers WHERE State = ‘NY’”);
    qd.Create( _T(“My Querydef”), strSQL );

    // Construct a CDaoRecordset-derived object
    // and open it based on the querydef
    CPublisherSet rsPub( pdb );
    rsPub.Open( &qd );
}
catch( CDaoException* e )
// ...

To create a recordset without a querydef

  1. Construct a CDaoRecordset object.

  2. Call the recordset object's Open member function. Specify an SQL string, or rely on the one that the wizard creates.

For an example in code, see the article DAO Recordset: Creating Recordsets.

The SQL statement for the recordset is one of the following:

In either case, your SQL string can consist of any of the following:

For details, see CDaoRecordset::Open. For related information, see the topic "Recordset Object" in DAO Help.

For information about SQL, see the topic "SQL Property" in DAO Help.

For information about the SQL syntax used by DAO, see the topic "Comparison of Microsoft Jet Database Engine SQL and ANSI SQL" in DAO Help.

See Also   DAO: Where Is..., DAO Recordset, DAO Querydef, DAO Record Field Exchange (DFX), DAO Queries: SQL for DAO, DAO Queries