Finding Records Using the Seek Method

You can use the Recordset object's Seek method to locate a record based on an index. If you don't specify the index before searching, the primary key will be used. If the record is found, the current row position is changed to that row. The syntax of the Seek method looks like this:

recordset.Seek KeyValues, SeekOption

The first argument of the Seek method specifies the key values you want to find. The second argument specifies the type of comparison to be made between the columns of the index and the corresponding KeyValues.

The procedure in Hands-On 13-11 uses the Seek method to find the first company with an entry in the Region field equal to "SP":

rst.Seek "SP", adSeekFirstEQ

Part II

To find the last record that meets the same condition, use the following statement:

rst.Seek "SP", adSeekLastEQ

The type of Seek to execute is specified by the constants shown in Table 13-1.

Table 13-1: Seek constants

Constant

Value

Description

adSeekFirstEQ

1

Seeks the first key equal to KeyValues.

adSeekLastEQ

2

Seeks the last key equal to KeyValues.

adSeekAfterEQ

4

Seeks either a key equal to KeyValues or just after where that match would have occurred.

adSeekAfter

8

Seeks a key just after where a match with KeyValues would have occurred.

adSeekBeforeEQ

16

Seeks either a key equal to KeyValues or just before where that match would have occurred.

adSeekBefore

32

Seeks a key just before where a match with KeyValues would have occurred.

The Seek method is recognized only by the Microsoft Jet 4.0 databases. To determine whether the Seek method can be used to locate a row in a recordset, use the Recordset object's Supports method. This method determines whether a specified Recordset object supports a particular type of feature. The Boolean value of True indicates that the feature is supported; False indicates that it is not.

' find out if the recordset supports the Seek method MsgBox rst.Supports(adSeek)

0 0

Post a comment