|
-
Nov 29th, 2006, 06:08 PM
#1
Thread Starter
Lively Member
[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.
-
Nov 29th, 2006, 06:31 PM
#2
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:
myComboBox.DisplayMember = "Activity.Activity" 'The name of the column containing the data to display.
myComboBox.ValueMember = "Activity.Activity" 'The name of the column contain the data to get vis the SelectedValue property.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|