ADO 2.7 Samples

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.

'BeginFindVB

    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string

Public Sub FindX()

     ' connection and recordset variables
    Dim Cnxn As New ADODB.Connection
    Dim rstTitles As New ADODB.Recordset
    Dim strCnxn As String
    Dim strSQLTitles As String
    
     ' record variables
    Dim mark As Variant
    Dim count As Integer
    
     ' open connection
    Set Cnxn = New ADODB.Connection
    strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; "
    Cnxn.Open strCnxn
       
    ' open recordset with default parameters which are
    ' sufficient to search forward through a Recordset
    Set rstTitles = New ADODB.Recordset
    strSQLTitles = "SELECT title_id FROM titles"
    rstTitles.Open strSQLTitles, Cnxn, adOpenStatic, adLockReadOnly, adCmdText

    count = 0
    rstTitles.Find "title_id LIKE 'BU%'"
    
    Do While Not rstTitles.EOF
        'continue if last find succeeded
       Debug.Print "Title ID: "; rstTitles!title_id
        'count the last title found
       count = count + 1
        ' note current position
       mark = rstTitles.Bookmark
       rstTitles.Find "title_id LIKE 'BU%'", 1, adSearchForward, mark
        ' above code skips current record to avoid finding the same row repeatedly;
        ' last arg (bookmark) is redundant because Find searches from current position
    Loop
    
    Debug.Print "The number of business titles is " & count
    
     ' clean up
    rstTitles.Close
    Cnxn.Close
    Set rstTitles = Nothing
    Set Cnxn = Nothing

End Sub
'EndFindVB

See Also

Find Method | Recordset Object

© 1998-2001 Microsoft Corporation. All rights reserved.