本范例使用 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