Seek Method Example (VC++)

This example queries you for a product ID, seeks the corresponding record, and then displays information about the product.

See How to Build the DAO Method Samples.

If you have installed the DAO SDK, see InstallDirectory\DAOSDK\SAMPLES\Seek.cpp.

The following example uses Seek to locate the first record in the Publishers table where the PubID field is 3, using the existing primary key index.

CdbDBEngine dben;
CdbDatabase dbsBiblio;
CdbRecordset rstPublishers;

dbsBiblio = dben.OpenDatabase(_T("Biblio.mdb"));
rstPublishers = dbsBiblio.OpenRecordset(_T("Publishers"));
rstPublishers.SetIndex(_T("PrimaryKey"));
rstPublishers.Seek(_T("="), 1, COleVariant((long) 3));
if (rstPublishers.GetNoMatch())
 {
 //...
 }

The following example uses the OpenDatabase method to directly open an installable ISAM database and then uses Seek to locate a record in a table in that database.

CdbDBEngine dben;
CdbDatabase dbsFoxData;
CdbRecordset rstParts;
CdbBookmark bk;
COleVariant var;

dbsFoxData = dben.OpenDatabase(_T("C:\\FoxData"),    FALSE, FALSE, _T("Fox 2.5;"));
rstParts = dbsFoxData.OpenRecordset(_T("parts.dbf"), dbOpenTable);

// Choose record order and Seek index.
rstParts.SetIndex(_T("PartNameIndex"));
bk = rstParts.GetBookmark();

// Search for first instance of a chosen part.
rstParts.Seek (_T("="), 1, COleVariant("Framis Lever", 
   VT_BSTRT));
if (rstParts.GetNoMatch())     // Test for success.
 rstParts.SetBookmark(&bk);   // Seek not successful.
else                        
// Seek worked; use current record.
 var = rstParts.GetField(_T("PartName"));