Seek Method

Applies To   Recordset object, Table-Type Recordset object.

Description

Locates the record in an indexed table-type Recordset object that satisfies the specified criteria for the current index and makes that record the current record (Microsoft Jet workspaces only).

Syntax

recordset.Seek comparison, key1, key2...key13

The Seek method syntax has the following parts.

Part

Description

recordset

An object variable that represents an existing table-type Recordset object that has a defined index as specified by the Recordset object's Index property.

comparison

One of the following string expressions: <, <=, =, >=, or >.

key1, key2...key13

One or more values corresponding to fields in the Recordset object's current index, as specified by its Index property setting. You can use up to 13 key arguments.


Remarks   You must set the current index with the Index property before you use Seek. If the index identifies a nonunique key field, Seek locates the first record that satisfies the criteria.

The Seek method searches through the specified key fields and locates the first record that satisfies the criteria specified by comparison and key1. Once found, it makes that record current and sets the NoMatch property to False. If the Seek method fails to locate a match, the NoMatch property is set to True, and the current record is undefined.

If comparison is equal (=), greater than or equal (>=), or greater than (>), Seek starts at the beginning of the index and searches forward.

If comparison is less than (<) or less than or equal (<=), Seek starts at the end of the index and searches backward. However, if there are duplicate index entries at the end of the index, Seek starts at an arbitrary entry among the duplicates and then searches backward.

You must specify values for all fields defined in the index. If you use Seek with a multiple-column index, and you don't specify a comparison value for every field in the index, then you cannot use the equal (=) operator in the comparison. That's because some of the criteria fields (key2, key3, and so on) will default to Null, which will probably not match. Therefore, the equal operator will work correctly only if you have a record which is all Null except the key you're looking for. It's recommended that you use the greater than or equal (>=) operator instead.

The key1 argument must be of the same field data type as the corresponding field in the current index. For example, if the current index refers to a number field (such as Employee ID), key1 must be numeric. Similarly, if the current index refers to a Text field (such as Last Name), key1 must be a string.

There doesn't have to be a current record when you use Seek.

You can use the Indexes collection to enumerate the existing indexes.

To locate a record in a dynaset- or snapshot-type Recordset that satisfies a specific condition that is not covered by existing indexes, use the Find methods. To include all records, not just those that satisfy a specific condition, use the Move methods to move from record to record.

You can't use the Seek method on a linked table because you can't open linked tables as table-type Recordset objects. However, if you use the OpenDatabase method to directly open an installable ISAM (non-ODBC) database, you can use Seek on tables in that database.

In an ODBCDirect workspace, the Find and Seek methods are not available on any type of Recordset 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.

See Also   AbsolutePosition property, BOF, EOF properties, FindFirst, FindLast, FindNext, FindPrevious methods, Index object, Index property, Move method, MoveFirst, MoveLast, MoveNext, MovePrevious methods, NoMatch property, OpenDatabase method.

Example

This example demonstrates the Seek method by allowing the user to search for a product based on an ID number.

Sub SeekX()

    Dim dbsNorthwind As Database
    Dim rstProducts As Recordset
    Dim intFirst As Integer
    Dim intLast As Integer
    Dim strMessage As String
    Dim strSeek As String
    Dim varBookmark As Variant

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    ' You must open a table-type Recordset to use an index,
    ' and hence the Seek method.
    Set rstProducts = _
        dbsNorthwind.OpenRecordset("Products", dbOpenTable)

    With rstProducts
        ' Set the index.
        .Index = "PrimaryKey"

        ' Get the lowest and highest product IDs.
        .MoveLast
        intLast = !ProductID
        .MoveFirst
        intFirst = !ProductID

        Do While True
            ' Display current record information and ask user
            ' for ID number.
            strMessage = "Product ID: " & !ProductID & vbCr & _
                "Name: " & !ProductName & vbCr & vbCr & _
                "Enter a product ID between " & intFirst & _
                " and " & intLast & "."
            strSeek = InputBox(strMessage)

            If strSeek = "" Then Exit Do

            ' Store current bookmark in case the Seek fails.
            varBookmark = .Bookmark

            .Seek "=", Val(strSeek)

            ' Return to the current record if the Seek fails.
            If .NoMatch Then
                MsgBox "ID not found!"
                .Bookmark = varBookmark
            End If
        Loop

        .Close
    End With

    dbsNorthwind.Close

End Sub
Example (Microsoft Access)

The following example creates a new Index object on an Employees table. The new index consists of two fields, LastName and FirstName. The procedure then uses the Seek method to find a specified record.

Sub NewIndex()
    Dim dbs As Database, tdf As TableDef, idx As Index
    Dim fldLastName As Field, fldFirstName As Field, rst As Recordset
    
    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to Employees table.
    Set tdf = dbs.TableDefs!Employees
    ' Return Index object that points to new index.
    Set idx = tdf.CreateIndex("FullName")
    ' Create and append index fields.
    Set fldLastName = idx.CreateField("LastName", dbText)
    Set fldFirstName = idx.CreateField("FirstName", dbText)
    idx.Fields.Append fldLastName
    idx.Fields.Append fldFirstName
    ' Append Index object.
    tdf.Indexes.Append idx
    tdf.Indexes.Refresh
    ' Open table-type Recordset object.
    Set rst = dbs.OpenRecordset("Employees")
    ' Set current index to new index.
    rst.Index = idx.Name
    ' Specify record to find.
    rst.Seek "=", "Fuller", "Andrew"
    If rst.NoMatch Then
        Debug.Print "Seek unsuccessful!"
    Else
        Debug.Print "Seek successful."
    End If
    rst.Close
    Set dbs = Nothing
End Sub
Example (Microsoft Excel)

This example opens Product.dbf (a dBASE IV table located in the C:\Program Files
\Common Files\Microsoft Shared\MSquery folder), locates a record, and then copies the record's values into cells B2:C2 on Sheet1. (In Windows NT, Product.dbf is located in the C:\Windows\Msapps\Msquery folder.)

Dim db As Database, rs As Recordset
Sheets("Sheet1").Activate
Set db = OpenDatabase("C:\Program Files\Common Files\Microsoft Shared\MSquery", _
        False, False, "dBASE IV")
Set rs = db.OpenRecordset("PRODUCT.DBF", dbOpenTable)
rs.Index = "PRODUCT"
rs.Seek "=", "1"
If rs.NoMatch Then
    MsgBox "Couldn't find any records"
Else
    ActiveSheet.Cells(2, 2) = rs.Fields("CATEGORY").Value
    ActiveSheet.Cells(3, 2) = rs.Fields("PROD_NAME").Value
End If
rs.Close
db.Close