Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
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-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated