FindFirst, FindLast, FindNext, FindPrevious Methods

See Also      Applies To

Locates the first, last, next, or previous record in a dynaset- or snapshot-type Recordset object that satisfies the specified criteria and makes that record the current record (Microsoft Jet workspaces only).

Syntax

VOIDFindFirst( LPCTSTRpstrCriteria );

VOIDFindLast( LPCTSTRpstrCriteria );

VOIDFindNext( LPCTSTRpstrCriteria );

VOIDFindPrevious( LPCTSTRpstrCriteria );

Parameters

Type Argument Description
LPCTSTR pstrCriteria A pointer to a string expression (like the WHERE clause in an SQL statement without the word WHERE) used to locate the record.

Remarks

If you want to include all the records in your search — not just those that meet a specific condition — use the Move methods to move from record to record. To locate a record in a table-type CdbRecordset, use the Seek method.

If a record matching the criteria isn't located, the current record pointer is unknown, and the NoMatch property is set to True. If recordset contains more than one record that satisfies the criteria, FindFirst locates the first occurrence, FindNext locates the next occurrence, and so on.

Each of the Find methods begins its search from the location and in the direction specified in the following table.

Find method Begins searching at Search direction
FindFirst Beginning of recordset End of recordset
FindLast End of recordset Beginning of recordset
FindNext Current record End of recordset
FindPrevious Current record Beginning of recordset

When you use the FindLast method, the Microsoft Jet database engine fully populates your CdbRecordset before beginning the search, if this hasn't already happened.

Using one of the Find methods isn't the same as using a Move method, however, which simply makes the first, last, next, or previous record current without specifying a condition. You can follow a Find operation with a Move operation.

Always check the value of the NoMatch property to determine whether the Find operation has succeeded. If the search succeeds, NoMatch is False. If it fails, NoMatch is True and the current record isn't defined. In this case, you must position the current record pointer back to a valid record.

Using the Find methods with Microsoft Jet-connected ODBC-accessed recordsets can be inefficient. You may find that rephrasing your criteria to locate a specific record is faster, especially when working with large recordsets.

In an ODBCDirect workspace, the Find and Seek methods are not available on any type of CdbRecordset object, because executing a Find or Seek through an ODBC connection is not very efficient over the network. Instead, you should design the query (that is, using the source argument to the OpenRecordset method) with an appropriate WHERE clause that restricts the returned records to only those that meet the criteria you would otherwise use in a Find or Seek method.

When working with Microsoft Jet-connected ODBC databases and large dynaset-type CdbRecordset objects, you might discover that using the Find methods or using the Sort or Filter property is slow. To improve performance, use SQL queries with customized ORDER BY or WHERE clauses, parameter queries, or CdbQueryDef objects that retrieve specific indexed records.

Use the U.S. date format (month-day-year) when you search for fields containing dates, even if you're not using the U.S. version of the Microsoft Jet database engine; otherwise, the data may not be found. Use the Microsoft Visual Basic Format function to convert the date. For example:

rstEmployees.FindFirst "HireDate > #" _
   & Format(mydate, 'm-d-yy' ) & "#"

If pstrCriteria is composed of a string concatenated with a non-integer value, and the system parameters specify a non-U.S. decimal character such as a comma (for example, strSQL = "PRICE > " & lngPrice, and lngPrice = 125,50), an error occurs when you try to call the method. This is because during concatenation, the number will be converted to a string using your system's default decimal character, and Microsoft Jet SQL only accepts U.S. decimal characters.

Notes

Usage

#include <afxole.h>
#include <dbdao.h>
...
CdbBookmark         bmHere;
CdbRecordset      rstCustomers;
...
// Save current record position.
bmHere = rstCustomers.GetBookmark();   
// Search for a name in the recordset.
rstCustomers.FindFirst(_T("Name = 'Ms. Schmidt'"));
// If not found go back to saved record position.
if (rstCustomers.GetNoMatch()) 
   rstCustomers.SetBookmark( &bmHere );