DAO Recordset: Creating Recordsets
| Overview | How Do I | FAQ | Sample | | ODBC Driver List
This article explains the process of creating recordset objects based either on class itself or on a class derived from CDaoRecordset.
You can use recordset objects in two ways:
Create recordsets directly from class CDaoRecordset and bind record fields dynamically.
For information about why and how to bind records dynamically, see the article DAO Recordset: Binding Records Dynamically.
Use AppWizard or ClassWizard to derive your own custom recordset class from and use DAO Record Field Exchange (DFX) to manage binding record data to recordset class field data members.
For information about deriving recordset classes with the wizards, see the articles Overview: Creating a Program That Supports a Database and ClassWizard: Creating a Recordset Class.
For information about using DFX, see the article DAO Record Field Exchange (DFX).
In either case, you can base your recordset objects on a query defined by a object or a 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 member function.
The following table describes the characteristics of a recordset, depending on how you create it:
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
Create the querydef, as described in Creating a Query with a Querydef, or use an existing querydef object.
Call the recordset object's 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
Call the recordset object's 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:
The default SQL string that you established when you created the recordset class using AppWizard or ClassWizard. If you pass NULL for the lpszSQL parameter to Open, the recordset uses the default SQL string. The recordset obtains the default SQL string by calling its own member function. The default SQL string is coded as the value returned by that function rather than being stored in a data member.
An SQL string that you pass in your call to Open. This string overrides the default SQL string defined in the recordset class.
In either case, your SQL string can consist of any of the following:
A SELECT statement of the basic form:
SELECT column-list FROM table-list
One or more tabledef and/or querydef names, separated by commas, which base the query on tables and/or queries. This list is usually based on choices you made with one of the wizards. The query selects all columns (fields) in the tables/queries, unless you modify it.
For details, see . 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