ADO Seek Method

Home | About me | EXCEL VB Programming (XL97-2003) | ACCESS Programming | EXCEL VB.Net Programming | EXCEL Spreadsheet Functions Material Management  |  Guestbook
 

ADO Seek Method

The Seek method searches the index of a Recordset to find a record that matches the values specified in the keyvalues parameter. If there is a match, the pointer will point to the record specified by the seekoption parameter. If there is no match, the record pointer will be placed at the end of the Recordset.

Note:
To use this method, the provider must support this method and the use of indexes on a Recordset (the Index property). Use the Supports method to determine whether the provider supports seek and indexes.

Set the Index property to the desired index before executing this method.

This method can only be used with server-side cursors (not supported when the CursorLocation property value is adUseClient).

This method can only be used when the Recordset object was opened with the CommandTypeEnum value of adCmdTableDirect.

Syntax

recordsetobj.Seek keyvalues,seekoption

 
Parameter

Description

keyvalues Required. An array of values to compare with the values in each column 
seekoption Required. A SeekEnum value that specifies the type of seek


Example

Option Compare Database
Option Explicit

Function SeekRecord()
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset

    Set conn = New ADODB.Connection
    Set rst = New ADODB.Recordset

    'Set the connection properties and open the connection.
    With conn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "F:\Mydata\northwind.mdb"
        .Open
    End With

    With rst
        'Select the index used in the recordset.
        .Index = "PrimaryKey"

        'Set the location of the cursor service.
        .CursorLocation = adUseServer

        'Open the recordset.
        .Open "Order Details", conn, adOpenKeyset, _
          adLockOptimistic, adCmdTableDirect

        'Find the customer order where OrderID = 10255 and ProductID = 16.
        .Seek Array(10255, 16), adSeekFirstEQ

        'If a match is found, print the quantity of the customer order.
        If Not rst.EOF Then
            Debug.Print rst.Fields("Quantity").Value
        End If
    End With
End Function
 

SeekEnum Values

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