19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

336 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

[Run, <strong>and</strong> click on Order by Population.]<br />

[Click on Show Currency, <strong>and</strong> then click on the Next navigator arrow six times.]<br />

The program in Example 1 of Section 11.1 searched a table for a specific record by looping<br />

through all the records. Whereas this technique is fine for small tables, it is not efficient<br />

for searches of large tables. <strong>Visual</strong> <strong>Basic</strong> provides a better way with Find methods.<br />

■ FIND METHODS<br />

Suppose a table has been attached to the data control Data1, <strong>and</strong> an SQL statement has been<br />

used to create <strong>and</strong> order a recordset. Then a statement of the form<br />

Data1.RecordSet.FindFirst criteria<br />

starts at the beginning of the recordset, searches for the first record in the recordset that<br />

satisfies the criteria, <strong>and</strong> makes that record the current record. (Here, criteria is a string just<br />

like a criteria phrase that follows WHERE in an SQL statement.) The related methods Find-<br />

Last, FindNext, <strong>and</strong> FindPrevious function as their names suggest. (FindLast starts at the end<br />

of the recordset. FindNext <strong>and</strong> FindPrevious start at the current record.) For instance, suppose<br />

an SQL statement ordered the cities alphabetically by name. The following statements<br />

<strong>and</strong> their outcomes show the effects of the various Find methods. (In each case, assume that<br />

the current record is Mexico City.)<br />

Statement New Current Record<br />

datCities.Recordset.FindFirst “pop2015 < 20” Beijing<br />

datCities.Recordset.FindLast “pop2015 < 20” Tianjin<br />

datCities.Recordset.FindNext “pop2015 < 20” New York<br />

datCities.Recordset.FindPrevious “pop2015 < 20” Los Angeles<br />

<strong>Visual</strong> <strong>Basic</strong> has two properties, NoMatch <strong>and</strong> Bookmark, that help when a Find method<br />

fails to locate a suitable record.<br />

If BkMk is a string variable, a statement of the form<br />

BkMk = Data1.Recordset.Bookmark<br />

assigns the location of the current record to the variable BkMk. When desired, the statement<br />

Data1.Recordset.Bookmark = BkMk<br />

will return to the original location in the table.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!