none
How to populate Combo box with Data from access

    Question

  • Hey Im trying to pooulate a combo box in vb.net 2008 with information from my access database, but I have no idea how to do thi :S im trying to get it so that  the customer names from the database appear in the combo box this is as far as i've gotten for my form load
     dbprovider = "PROVIDER = microsoft.jet.OLEDB.4.0;"
        dbsource = (" Data Source = C:\Users\SEUN\Desktop\Addressbook.mdb")
        con.ConnectionString = dbprovider & dbsource
    
        con.Open()
    
        sql = "Select * FROM tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Addressbook")
    
        con.Close()
    
    Sunday, May 01, 2011 4:47 PM

Answers

  • there are lots of threads on the Forums - for example, to the right here is a Related Topics pane for some of them.

     

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/328648ba-0407-4b4c-b625-0e39c3f1d6d0

    Sunday, May 01, 2011 5:26 PM
  • Yes, you need assign ComboBox a DataSource, and specify its DisplayMember and ValueMember. Sample code here is for SQL database, for Acess database only need change SQL component to Oledb component.
    Dim con As SqlConnection = New SqlConnection( _
    ...........)
      con.Open()
      Dim constr As String = "......"
      Dim cmd As SqlCommand = New SqlCommand(constr, con)
      Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
      Dim ds As DataSet = New DataSet()
      da.Fill(ds, "table")
      Me.ComboBox1.DataSource = ds.Tables("table").DefaultView
      Me.ComboBox1.DisplayMember = "customer name"
      Me.ComboBox1.ValueMember = "....."
      con.Close()
    

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 03, 2011 7:49 AM

All replies

  • there are lots of threads on the Forums - for example, to the right here is a Related Topics pane for some of them.

     

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/328648ba-0407-4b4c-b625-0e39c3f1d6d0

    Sunday, May 01, 2011 5:26 PM
  • Yes, you need assign ComboBox a DataSource, and specify its DisplayMember and ValueMember. Sample code here is for SQL database, for Acess database only need change SQL component to Oledb component.
    Dim con As SqlConnection = New SqlConnection( _
    ...........)
      con.Open()
      Dim constr As String = "......"
      Dim cmd As SqlCommand = New SqlCommand(constr, con)
      Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
      Dim ds As DataSet = New DataSet()
      da.Fill(ds, "table")
      Me.ComboBox1.DataSource = ds.Tables("table").DefaultView
      Me.ComboBox1.DisplayMember = "customer name"
      Me.ComboBox1.ValueMember = "....."
      con.Close()
    

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 03, 2011 7:49 AM
  • thanks where do i put this code? in my combo box or my load form? also what do i put in
     Me.ComboBox1.ValueMember = "....."
    what goes inbetween the "....." ??? t

    thank you for your help
    Monday, May 09, 2011 3:53 PM
  • normally you would put it right after the da.Fill line

    the DisplayMember property is the column name you want displayed in the ComboBox

    the ValueMember property is the column name you want to use as the value from another (or the same) column to use based on the selection of the DisplayMember

    for example, say you have 2 columns: ID and Name

    if you set the DisplayMember to the Name column and the ValueMember to the ID column, whenever you select a name from the ComboBox, you can have the ID for that person display in a label, messagebox, etc.

     

    Monday, May 09, 2011 7:09 PM