Find Method Example (VB)

Microsoft ActiveX Data Objects (ADO)

Find Method Example (VB)

This example uses the Recordset object's Find method to locate and count the number of business titles in the Pubs database. The example assumes the underlying provider does not support similar functionality.

Public Sub Main()
    FindX
End Sub

Public Sub FindX()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim mark As Variant
Dim count As Integer

count = 0
cnn.Open "DSN=Pubs; Provider=MSDASQL; uid=sa; pwd=;"
rst.Open "SELECT title_id FROM titles", cnn, _
            adOpenStatic, adLockReadOnly, adCmdText

' The default parameters are sufficient to search forward
' through a Recordset.

rst.Find "title_id LIKE 'BU%'"

' Skip the current record to avoid finding the same row repeatedly.
' The bookmark is redundant because Find searches from the current
' position.

Do While rst.EOF <> True    'Continue if last find succeeded.
    Debug.Print "Title ID: "; rst!title_id
    count = count + 1        'Count the last title found.
    mark = rst.Bookmark      'Note current position.
    rst.Find "title_id LIKE 'BU%'", 1, adSearchForward, mark
Loop

rst.Close
cnn.Close
Debug.Print "The number of business titles is " & count

End Sub