Image Map of Navigational Panel to Home / Contents / Search Avoiding Bound Controls

The code behind BindList looks like this...

Image of line

  Sub BindList (lst As Control, db As Database, ByVal sTableName As String, 
                                                ByVal sIDField As String, 
                                                ByVal sDisplayField As String)
    Dim sSQL As String
    Dim ssGet As snapshot
    Dim bContinue As Integer
   
    On Error GoTo BindListErr
    ' Check the class of the control passed
    If TypeOf lst Is ListBox Then
      bContinue = True
    ElseIf TypeOf lst Is ComboBox Then
      bContinue = True
    Else
      bContinue = False
    End If
    ' Bind a List or ComboBox to a recordset
    If bContinue Then
      lst.Clear
      sSQL = "SELECT DISTINCT " & sIDField & " AS ID, 
                              " & sDisplayField & " AS Display FROM " & sTableName
      
      ' Order the contents of the List by sorting the
      ' recordset, itemdata gets sluggish if your list
      ' is sorted - so we sort the data instead
      sSQL = sSQL & " ORDER BY " & sDisplayField
      Set ssGet = db.CreateSnapshot(sSQL)
      
      ' Add each record to the List
      Do While Not ssGet.EOF
        lst.AddItem ssGet!Display
        ' Set the itemData of the last added item
        lst.itemdata(lst.NewIndex) = ssGet!ID
        ssGet.MoveNext
      Loop
      ssGet.Close
      Set ssGet = Nothing
    Else
      ' This msgbox is designed for developers
      MsgBox "Invalid control Argument to BindList."
    End If
  BindListEnd:
    Exit Sub
  BindListErr:
    ' Some pretty stock error checking
    MsgBox "Error encountered binding Listbox to database." & BIND_ErrMsg(), 48
    Resume BindListEnd
  End Sub


Image of arrow to previous article

Image of line

[HOME] [TABLE OF CONTENTS] [SEARCH]