Visual Basic Concepts
A common task while using the DataRepeater control is to maintain a history of viewed records. The history can be displayed in a ListBox control. Any time the user clicks on an item in the history, the DataRepeater control displays that record in its client area. As with other data-aware controls (such as the DataGrid control), you (the developer) must store and retrieve bookmarks for each record you wish to track.
Important Bookmarks are valid only for the life of a recordset, and are not persistable or interchangeable across recordsets.
The following step-by-step creates a history by first storing the bookmarks of viewed records in an array. A value associated with the record is also added to a ListBox control. When the user clicks an item in the ListBox control, the ListIndex of the clicked item is used to retrieve the bookmark of the record. Setting the VisibleRecords property to the bookmark causes the record to appear in the DataRepeater control.
The step-by-step builds upon the project created in "Using the DataRepeater Control."
To create a history of viewed records
On the Project menu, click References to open the References dialog box. Check Microsoft ActiveX Data Objects 2.0 Library, and click OK to close the dialog box.
Option Explicit
Private varBookmarks() As Variant
Private rsProducts As ADODB.Recordset
Private Sub Form_Load()
ReDim varBookmarks(0)
Set rsProducts = adodc1.Recordset
End Sub
Private Sub DataRepeater1_CurrentRecordChanged()
' The CurrentRecordChanged event occurs immediately after the form
' is opened. At that point, the UBound(varBookmarks) = 0, and no
' records have been added. Thus you must add the first record to
' the history list, and its bookmark to the array. Then increment
' the array index to 1 and exit the sub. The next time a record is
' clicked, this code will be ignored.
Dim iUpper As Integer ' Upper bound of bookmark array.
Dim i As Integer ' Counter for ListBox
If UBound(varBookmarks) = 0 Then
lstHistory.AddItem rsProducts!ProductName
varBookmarks(0) = DataRepeater1.CurrentRecord
ReDim Preserve varBookmarks(1)
Exit Sub
End If
' The code below now executes when the user clicks a record.
' First check the listbox to ensure the clicked record hasn't
' already been added to the history list. If so, exit.
For i = 0 To lstHistory.ListCount - 1
If lstHistory.List(i) = rsProducts!ProductName _
Then Exit Sub
Next I
' If no duplicates, then add the record to the history, and
' cache the bookmark in the array.
lstHistory.AddItem rsProducts!ProductName
varBookmarks(UBound(varBookmarks)) = DataRepeater1.CurrentRecord
' Increment the array.
ReDim Preserve varBookmarks(UBound(varBookmarks) + 1)
End Sub
Private Sub lstHistory_Click()
Debug.Print lstHistory.Text, lstHistory.ListIndex
DataRepeater1.VisibleRecords(1) = _
varBookmarks(lstHistory.ListIndex)
End Sub