2

I am accessing data from Access MDB file using my application developed in VB .net 2010. I want to get values from a particular table-column to be populated in a Combo Box dropdown.

I followed a similar thread @ C# Adding data to combo-box from MS Access table but as the code is in C#, I was not able to successfully implement it. And reverted back to my previous code.

Because the Table will be updated by another application as well, every time I use my utility I need to get the latest data from that table. Hence I would like to have a feature that could get all available id from table.

Below is what I have been trying to do..

'below declared at class level. 
' Dim cnnOLEDB As New OleDbConnection
' Dim cmdOLEDB As New OleDbCommand

cnnOLEDB.Open()

cmdOLEDB.CommandText = "select unique_id from tag_data"
cmdOLEDB.Connection = cnnOLEDB

Dim rdrOLEDB2 As OleDbDataReader = cmdOLEDB.ExecuteReader

If rdrOLEDB2.Read = True Then
    TagComboBox1.DataSource = rdrOLEDB2.Item(0).ToString

    TagComboBox1.ValueMember = "unique_id"
    TagComboBox1.DisplayMember = "unique_id"

End If
rdrOLEDB2.Dispose()
rdrOLEDB2.Close()
cnnOLEDB.Close()
Community
  • 1
  • 1
DK2014
  • 171
  • 3
  • 20

1 Answers1

2

I would recommend filling a DataTable with your query. Then you can set the DataSource of the combobox, ValueMember & the DisplayMember.

 Dim dt As New DataTable
 Dim query As String = "select unique_id from tag_data"

 Using connection As New OleDbConnection("your connection string")
  Using command As New OleDbCommand(query, connection)
    Using adapter As New OleDbDataAdapter(command)
        connection.Open()
        adapter.Fill(dt)
        connection.Close()
    End Using
  End Using
 End Using

 If dt.Rows.Count > 0 Then
  TagComboBox1.DataSource = dt
  TagComboBox1.ValueMember = "unique_id"
  TagComboBox1.DisplayMember = "unique_id"
 End If
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • Thanks for your quick response. I am getting "Array bounds cannot appear in type specifiers." error for the query text inside; --> Dim cmd As Oledb.OledbCommand("select unique_id from tag_data") – DK2014 Jul 30 '14 at 13:56
  • Awesome dude.. its a direct hit.. worked great..I tested adding more values to that table and the combobox lists newly added values as well..You made my day.. – DK2014 Jul 30 '14 at 15:27
  • Can I use similar method to get data from another column into a Textbox on the .Net form? The table is same and the column is Tag_Text. What I want to do now is on the SelectedIndex of above TagComboBox1 value, I want to get the relevant value from the table tag_data into TagTextBox. I noticed that Textbox class does not have a Datasource property – DK2014 Jul 30 '14 at 15:43
  • Change your select to include the fields that you need, then look inside the datatable for the column that you need. – Trevor Jul 30 '14 at 16:06
  • Hi, I want to edit below such that for each new added value in the tag_data table, the code also assigns a new index (selectedIndex) to the combobox value. [CODE] If dt.Rows.Count > 0 Then TagComboBox1.DataSource = dt TagComboBox1.ValueMember = "unique_id" TagComboBox1.DisplayMember = "unique_id" 'I added below, What I want is to have an incremented number every 'time reocrd get updated in the source table. TagComboBox1.SelectedIndex = 0 End If [/CODE] – DK2014 Jul 31 '14 at 13:12
  • Could you possible ask this in a new question as this issue has been resolved. This way if others need it, they can find the solution. – Trevor Jul 31 '14 at 13:52
  • Thanks..I have added it as a new question @ following link http://stackoverflow.com/questions/25061559/how-to-loop-through-ms-access-table-column-values-pulled-in-a-combo-box-vb-net – DK2014 Jul 31 '14 at 15:10