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.
'BeginCompareBookmarksVB 'To integrate this code 'replace the data source and initial catalog values 'in the connection string Public Sub CompareBookmarksX() ' recordset and connection variables Dim rstAuthors As ADODB.Recordset Dim Cnxn As ADODB.Connection Dim strSQLAuthors As String Dim strCnxn As String ' comparison variables Dim Count As Integer Dim target As Variant Dim result As Long Dim strAnswer As String Dim strTitle As String strTitle = "CompareBookmarks Example" ' Open a connection Set Cnxn = New ADODB.Connection strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; " Cnxn.Open strCnxn ' Open recordset as a static cursor type recordset Set rstAuthors = New ADODB.Recordset strSQLAuthors = "SELECT * FROM Authors" rstAuthors.Open strSQLAuthors, Cnxn, adOpenStatic, adLockReadOnly, adCmdText Count = rstAuthors.RecordCount Debug.Print "Rows in the Recordset = "; Count ' Exit if an empty recordset If Count = 0 Then Exit Sub ' Get position between 0 and count -1 Randomize Count = (Int(Count * Rnd)) Debug.Print "Randomly chosen row position = "; Count ' Move row to random position rstAuthors.Move Count, adBookmarkFirst ' Remember the mystery row target = rstAuthors.Bookmark Count = 0 rstAuthors.MoveFirst ' Loop through recordset Do Until rstAuthors.EOF result = rstAuthors.CompareBookmarks(rstAuthors.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 strAnswer = "less than" Case adCompareEqual strAnswer = "equal to" Case adCompareGreaterThan strAnswer = "greater than" Case Else strAnswer = "in error comparing to" End Select 'show the results row-by-row Debug.Print "Row position " & Count & " is " & strAnswer & " the target." End If Count = Count + 1 rstAuthors.MoveNext Loop ' clean up rstAuthors.Close Cnxn.Close Set rstAuthors = Nothing Set Cnxn = Nothing End Sub 'EndCompareBookmarksVB
CompareBookmarks Method | CompareEnum | Recordset Object
© 1998-2001 Microsoft Corporation. All rights reserved.