Reposition Event Example

The example changes the selection of a list box based on data in the current record.

' In the Declarations section.
Dim Loading As Integer

Sub Form_Load ()
   Data1.DatabaseName = "C:\VB\BIBLIO.MDB"
   Data1.RecordSource = "SELECT DISTINCT State FROM Publishers"
   Loading = True                         ' Set the global variable.
   Data1.Refresh                          ' Open database & recordset.
   Do While Not Data1.Recordset.EOF
      If Not IsNull(Data1.Recordset(0)) Then  ' If not empty, then
         List1.AddItem Data1.Recordset(0) ' Add to the list
      End If
      Data1.Recordset.MoveNext            ' Move to next record.
   Loop
   Data1.RecordSource = "Publishers"      ' Change the RecordSource.
   Data1.Refresh                          ' Rebuild the recordset.
   Text1.DataField = "State"              ' Set field for text box.
   Loading = False                        ' Turn off global variable.
End Sub

Sub Data1_Reposition ()
   Dim I As Integer
   If Loading Then Exit Sub               ' If global variable is set.
   For I = 0 To List1.ListCount - 1       ' Check items in list box.
      If List1.List(I) = Data1.Recordset("State") Then
         List1.ListIndex = I              ' Select matching item.
         Exit For                         ' Stop looking through list.
      End If
   Next I
End Sub