CompareBookmarks Method Example (VB)

Microsoft ActiveX Data Objects (ADO)

CompareBookmarks Method Example (VB)

This example demonstrates the CompareBookmarks method. The relative value of bookmarks is seldom needed unless a particular bookmark is somehow special.

Designate a random row of a Recordset derived from the Authors table as the target of a search. Then display the position of each row relative to that target.

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