Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox Datasource - simplest way?

Status
Not open for further replies.

ribbons

Technical User
Apr 26, 2007
113
US
I'm using VB.NET 2005, a real newbie to .NET (used VB 6), and trying to finished an inherited application.

What is the simplest way to fill a combobox with values(1 column) from an Access DB table using a simple SQL string?
Will this work:

Code:
combobox1.Datasource = "SELECT Column1 from Table1"

I want the user to select from the list and have the report viewer display a report based on the selection. I have creating a query in the DB and used the smart task options to fill the combo box, but it quickly got complicated beyond me.
Any help or a point toward an answer would be appreciated.
Thanks in advance.

ribbons
 
It doesn't get any easier than code written by someone else. LOL!

Code:
    'You may want to put this first val in a module and make it 
    'changable via an AppSetting, INI file, or registry setting.

    Private strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Path\MyAccessDB.mdb"

    '-----------------------------------------

    Protected Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cmbComboBox1.DataSource = GetData("SELECT Column1, Conumn2 from Table1")
        cmbComboBox1.DisplayMember = "Column1"
        cmbComboBox1.ValueMember = "Column2"
    End Sub

    '-----------------------------------------

    Private Function GetData(ByVal queryString As String) As System.Data.DataTable
        Dim conn As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(strConn)
        Dim cmd As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        cmd.CommandText = queryString
        cmd.Connection = conn
        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = cmd
        Dim dsRet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dsRet)
        Return dsRet.Tables(0)
    End Function

Senior Software Developer
 
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim dt As DataTable
Dim SQLStr As String

conn = New OleDbConnection("connection string here")

conn.Open

SQLStr = "SELECT Column1 from Table1"

da = New OleDbDataAdapter(SQLStr, conn)

dt = New DataTable

da.Fill(dt)

conn.Close

conn.Dispose

conn = Nothing

da.Dispose

da=Nothing

ComboBox1.DataSource = dt

ComboBox1.DisplayMember = "Column1"

'note, this is not necessary for only one column
'you can use the DisplayMember instead.
ComboBox1.ValueMember = "Column1"



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
You guys are great! Wow, that's much more complicated that I thought it would be.

ribbons
 
On this same note, can I source multiple comboboxes like this:

ComboBox1.DataSource = dt

ComboBox1.DisplayMember = "Column1"

ComboBox2.Datasource = dt
ComboBox2.DisplayMember = "Column2"

If so, is there a way to make the contents of these columns DISTINCT?

ribbons
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top