CompareBookmarks メソッドの例 (VB)

この例では、CompareBookmarks メソッドの機能を示します。通常、ブックマークの相対的な値は、特別なブックマークでない限り、必要ではありません。

Authors テーブルから取得した Recordset の任意の行を、検索の対象として指定します。次に、その検索対象に対する各行の相対的な位置を表示します。

Public Sub Main()
    CompareBookmarksX
End Sub

Public Sub CompareBookmarksX()
Dim rst As ADODB.Recordset
Dim count As Integer
Dim target As Variant
Dim result As Long
Dim strAns As String
Dim strTitle As String
strTitle = "CompareBookmarks Example"

Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM Authors", _
            "DSN=Pubs;Provider=MSDASQL; uid=sa;pwd=;", _
            adOpenStatic, adLockReadOnly, adCmdText

count = rst.RecordCount
Debug.Print "Rows in the Recordset = "; count
If count = 0 Then Exit Sub   'Exit if an empty recordset

Randomize
count = (Int(count * Rnd))   'Get position between 0 and count-1
Debug.Print "Randomly chosen row position = "; count
rst.Move count, adBookmarkFirst 'Move row to random position
target = rst.Bookmark        'Remember the mystery row.

count = 0
rst.MoveFirst
Do While Not rst.EOF         'Loop through recordset
    result = rst.CompareBookmarks(rst.Bookmark, target)
    If result = adCompareNotEqual Then
        Debug.Print "Row "; count; ": Bookmarks are not equal."
    ElseIf result = adCompareNotComparable Then
        Debug.Print "Row "; count; ": Bookmarks are not comparable."
    Else
        Select Case result
            Case adCompareLessThan
            strAns = "less than"
            Case adCompareEqual
                strAns = "equal to"
            Case adCompareGreaterThan
                strAns = "greater than"
            Case Else
                strAns = "in error comparing to"
        End Select
        Debug.Print "Row position " & count & " is " & strAns & _
                        " the target."
    End If
    count = count + 1
    rst.MoveNext
Loop

rst.Close
End Sub