Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleLet the user select database records using a ComboBox
KeywordsComboBox, database records, select
CategoriesDatabase, Controls
 
When the form loads, the program fills the ComboBox with the values the user can select.
 
Private Sub Form_Load()
Dim dbname As String
Dim db As Database
Dim rs As Recordset

    ' Open the database.
    dbname = App.Path
    If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
    dbname = dbname & "data.mdb"
    
    Set db = OpenDatabase(dbname)
    Set rs = db.OpenRecordset( _
        "SELECT Name FROM People ORDER BY Name", _
        dbOpenSnapshot)

    ' Load the ComboBox.
    rs.MoveFirst
    Do While Not rs.EOF
        cboName.AddItem rs!Name
        rs.MoveNext
    Loop
    
    rs.Close
    db.Close
    
    ' Connect the Data control to the database.
    datPeople.DatabaseName = dbname

    ' Select the first choice.
    cboName.ListIndex = 0
End Sub
 
When the user selects an entry in the ComboBox, the program changes a Data control's select statement to select the new record. TextBoxes are bound to the Data control to display the data.
 
Private Sub cboName_Click()
    ' Load the selected record.
    datPeople.RecordSource = _
        "SELECT * FROM People WHERE Name='" & _
        cboName.Text & "'"
    datPeople.Refresh
End Sub
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated