DAO Recordset: Seeking and Finding
| Overview | How Do I | FAQ | Sample | | ODBC Driver List
This article explains how to use the and member functions of class . Topics covered include:
Using Seek
Using Find
These two mechanisms for locating records that meet certain criteria are used in different situations, as described in the following table.
Using Seek vs. Using Find
Seek and Find are not the only means of navigating in a recordset. You can also use:
For more information, see each CDaoRecordset member function in the Class Library Reference.
Using Seek
The member function lets you search for a record in a table-type recordset based on a table index. Two versions of the function provide for seeking based on:
Up to three specified keys, each of which represents a field that makes up part of the current index.
An array of keys, for indexes with four or more fields. Each key represents one of the fields. The array must contain at least one and no more than 13 keys.
In both versions, the search is based on a string containing a relational operator, such as "=" or ">=", in the lpszComparison parameter and the value specified in the first key.
For example, suppose the comparison operator is "=" and the first key is the value "Microsoft" (the first key being a Company Name field). Using the first version of Seek, you would find the first record that has a Company Name of "Microsoft". The found record becomes the current record. The following code illustrates how to use Seek:
// rs is a table-type recordset
try
{
// Set current index for recordset and
// save current position.
rs.SetCurrentIndex( _T("PartNameIndex") );
COleVariant varCurrentPos = rs.GetBookmark( );
// variant used as a key in Seek
COleVariant varKey (_T("Framis Lever"), VT_BSTRT);
// Find first record whose Part Name
// field is "Framis Lever".
if ( rs.Seek( _T("="), &varKey ) )
// Return to the saved position
rs.SetBookmark( varCurrentPos );
else
// Do something in response to Seek failure
}
catch( CDaoException* e )
{
e->Delete( );
}
This code seeks the first record whose Part Name field (the first field in the PartNameIndex index) is "Framis Lever" (whatever a framis lever is).
For more information, see the and member functions in the Class Library Reference. For related information about the underlying DAO functionality, see the following topics in DAO Help:
Seek Method
NoMatch Property
Index Object
Index Property
Using Find
The member function and its relatives, , , , and , let you search for a record in a dynaset-type or snapshot-type recordset. The Find member functions search from a location and in a direction as shown in the following table.
The Find Family of Functions
Find operation | Begin at | Search direction |
FindFirst | Beginning of recordset | End of recordset |
FindLast | End of recordset | Beginning of recordset |
FindNext | Current record | End of recordset |
FindPrev | Current record | Beginning of recordset |
The basic Find function takes two parameters:
The type of find: AFX_DAO_NEXT, AFX_DAO_PREV, AFX_DAO_FIRST, or AFX_DAO_LAST.
A filter — a string expression like the WHERE clause in an SQL statement (without the keyword), that specifies the criterion for finding. The expression can be compound, using AND, OR, and so on.
is a virtual function. This means you can, if necessary, override it to provide your own implementation. The other Find functions are all based on Find, so they use whatever functionality you provide in your override. You shouldn't normally need to override Find, however.
For details not discussed here about the Find member functions, see the individual functions, starting with . For related information about the underlying DAO functionality, see the topic "Positioning the Current Record Pointer with DAO" in DAO Help:
For example, suppose you have a dynaset-type recordset in which you want to find the first record with a State code of "NY":
// rs is a dynaset-type recordset previously opened
CString strCriteria = _T("STATE = ‘NY’");
try
{
if ( rs.FindFirst( strCriteria ) )
// Do something with the found record
rs.FindNext( strCriteria );
// ...
}
catch( CDaoException* e )
{
e->Delete( );
}
This code finds the first record that matches the criterion, then finds the next record that matches the criterion.
See Also DAO: Where Is..., DAO Recordset, DAO Recordset: Recordset Navigation, DAO Recordset: Bookmarks and Record Positions