Find メソッドの例 (VB)

この例では、Recordset オブジェクトの Find メソッドを使って、Pubs データベースのビジネス タイトルを検索して位置を定め、その数をカウントします。この例は、基になるプロバイダが類似の機能をサポートしていない場合を想定しています。

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