Results 1 to 2 of 2

Thread: [RESOLVED] ADO combo box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    71

    [RESOLVED] ADO combo box

    How can I populate a Bind a combo box to data stored in a table (tblActivity) using ADO via code?
    so far I have been able to connect to the database, the code below represent my progress so far




    Code:
    'created the variable conn that represents a new instance of 
    'OleDb.Connection Object
    
    Dim conn As New OleDb.OleDbConnection
        
    'created the variable DataSet that represents a new instance of the object DataSet
    Dim DataSet As New DataSet
    
    'create the variable DataAdapter as an instance of OleDB.OleDbDataApapter
     Dim DataAdapter As OleDb.OleDbDataAdapter
    
      
    
    Dim sql As String
       
    Public Sub PopulateActivity()
    
            conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" _
            & Application.StartupPath & "\TSDB.mdb"
    
            conn.Open()
    
            sql = "SELECT Activity FROM tblActivity"
    
            DataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
    
            DataAdapter.Fill(DataSet, "Activity")
    
    
    
            conn.Close()
    
    End Sub
    Last edited by cedtech23; Nov 30th, 2006 at 09:20 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ADO combo box

    Just note that you're using ADO.NET, not ADO. They are two quite different things, although they perform the same function.

    None of what came before has any bearing on your data-binding. You bind a DataTable to a ComboBox, but how the data came to be in the table is of no concern to the data-binding mechanism.
    VB Code:
    1. myComboBox.DisplayMember = "Activity.Activity" 'The name of the column containing the data to display.
    2. myComboBox.ValueMember = "Activity.Activity" 'The name of the column contain the data to get vis the SelectedValue property.
    3. myComboBox.DataSource = DataSet ' The object containing the data.
    Note that the DisplayMember and ValueMember are usually different columns. The most usual case is that the DisplayMember is a name or description while the ValueMember is the ID.

    I suggest that you go to the MSDN library and read about the DataSource, DisplayMember, ValueMember and SelectedValue properties of the ComboBox class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width