Tracking Records with Bookmarks and SelBookmarks

See Also

Bookmarks and SelBookmarks provide a means to track records. This is necessary when you program special functionality into an application, such as allowing the end user to manually select several discontiguous records, and performing a bulk update of the selected records. In that case, you will need to track which records have been selected, and you would therefore use the SelBookmarks collection and its properties.

Two functions, the CellText and CellValue methods, require bookmarks to perform properly.

Tracking User Selections

The SelBookmarks collection contains the bookmarks of all selected records. When the end user manually selects records (by holding down the CTRL key while clicking) the bookmark for each selected record is added to the collection. Using standard iteration methods, you can determine what has been selected, store the bookmarks (in case you need to restore a value), and perform the operation:

Dim i as Integer ' Counter
Dim intCount As Integer
intCount = DataGrid1.SelBookmarks.Count - 1
ReDim arrSelBK(intCount) ' Declare array to hold bookmarks.
For i = 0 To intCount
   ArrSelBK(i) = DataGrid1.SelBookmarks(i)
   ' Perform operations here. If the operations must be
   ' cancelled, exit the loop and use the array to 
   ' rollback the changes.
Next i

Programmatically Select Records by Adding to the SelBookmarks Collection

You can also programmatically select records by adding them to the collection. For example, you may have a grid that shows all of the orders by a certain customer. To highlight all of the records where the customer spent more than $100, filter the records, and add the resulting bookmarks to the SelBookmarks collection.

Dim rs As Recordset
Set rs = Adodc1.Recordset

While Not rs.EOF
   If rs!SupplierID = 12 Then
      DataGrid1.SelBookmarks.Add rs.Bookmark
   End If
   rs.MoveNext
Wend