By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

How do I create a combobox that works like MS Access's

Seth Schrock
Expert 2.5K+
P: 2,944
I am trying to create a front end program through Visual Studio 2012 using VB.Net for a database backend (both Access and SQL Server will be used for the backend). What I'm used to in MS Access is that the combo box has a Control Source property which tells it in which field to store the selected value. Then you have a Row Source property that allows you to query a second (related) table for possible values. I then bind the control to the primary key field of the related table, but show the text field. So I have two fields in the combobox, but the column sizes are 0 inches for the first column (makes it a hidden column) and then whatever size is needed to show the data that I want seen.

So far, I have been able to get the text for the list of options by using the DataSource property to list the binding source and then the DisplayMember property to select the field I want shown. I believe that the Text property under the DataBindings group would be the same as the control source in Access, however there are also SelectedItem and SelectedValue properties which I can't figure out what they are for.

I have read about multi-column comboboxes, but I'm not trying to display multiple columns, so I'm not sure if this is the route I need to follow.

Please let me know if my question is unclear.
Jun 21 '13 #1

✓ answered by zmbd

Someone correct me if I am wrong
vb.net is NOT my realm of expertese....

Seth,
From what I understand, unlike when you are doing this within the Access interface where the combobox can be linked to the data tablw, the combobox within vb.net has to have each item loaded into the control's list.

The one clear tutorial I found on this:

Note that the full article has step by step in the connection and the table setup etc; however, I think this is the main concept you're after:
Bind a ComboBox to a database lookup table in VB .NET
At run time, you still need to load and save the database data. In the form's Load event handler, call the data adapters' Fill methods to fill the DataSet. In this case, the program must load the UserTypes data first so it will be available when the Users table is loaded (the Users.UserType value must match a UserTypes.UserTypeId value).



' Load the data.
daUserTypes.Fill(dsUserData)
daUsers.Fill(dsUserData)



In the form's Closing event handler, the program saves changes to the Users table.


' Save any changes to the data.
daUsers.Update(dsUserData)

Share this Question
Share on Google+
5 Replies


zmbd
Expert Mod 5K+
P: 5,397
Someone correct me if I am wrong
vb.net is NOT my realm of expertese....

Seth,
From what I understand, unlike when you are doing this within the Access interface where the combobox can be linked to the data tablw, the combobox within vb.net has to have each item loaded into the control's list.

The one clear tutorial I found on this:

Note that the full article has step by step in the connection and the table setup etc; however, I think this is the main concept you're after:
Bind a ComboBox to a database lookup table in VB .NET
At run time, you still need to load and save the database data. In the form's Load event handler, call the data adapters' Fill methods to fill the DataSet. In this case, the program must load the UserTypes data first so it will be available when the Users table is loaded (the Users.UserType value must match a UserTypes.UserTypeId value).



' Load the data.
daUserTypes.Fill(dsUserData)
daUsers.Fill(dsUserData)



In the form's Closing event handler, the program saves changes to the Users table.


' Save any changes to the data.
daUsers.Update(dsUserData)
Jun 24 '13 #2

Seth Schrock
Expert 2.5K+
P: 2,944
I will give it a try, but I don't think that I do need to add the values at run-time as I already have the list of items being populated. I just can't get the equivalent to the Control Source property in Access to work. I think this is because I don't have a Bound To property.

Anyway, I'll give it a try and then let you know.
Jun 24 '13 #3

Seth Schrock
Expert 2.5K+
P: 2,944
Yippee! That worked. For easy future reference, here are the property settings in the order they appear in the categorized view:

Two binding sources, one for each table (in my case, I have a tblEmployee and tblEmployeeType table)

(DataBindings) SelectedValue = TblEmployeeBindingSource - EmployeeType
This is the foreign key field

DataSource = TblEmployeeTypeBindingSource
DisplayMember = EmpType
ValueMember = EmpTypeID
These are the fields in the One side of the relationship.

You do have to use code to make the data get filled into the combobox, but not the individual items like I had seen other instructions do. Instead the code just forces the combobox to pull the data from the table.

Thanks again Z.
Jun 24 '13 #4

zmbd
Expert Mod 5K+
P: 5,397
Cool-beans... I was taking a stab into a really dark portion of the cave!

Could you post the code you used to push the data or is that the same as covered within the link?
Jun 24 '13 #5

Seth Schrock
Expert 2.5K+
P: 2,944
Expand|Select|Wrap|Line Numbers
  1. 'TODO: This line of code loads data into the 'LT_TestDataSet.tblEmployeeType' table. You can move, or remove it, as needed.
  2.         Me.TblEmployeeTypeTableAdapter.Fill(Me.LT_TestDataSet.tblEmployeeType)
  3.         'TODO: This line of code loads data into the 'LT_TestDataSet.tblEmployee' table. You can move, or remove it, as needed.
  4.         Me.TblEmployeeTableAdapter.Fill(Me.LT_TestDataSet.tblEmployee)
It is covered in the link. The code was already there, but I can't remember if I typed it in before while I was trying to figure it out, or if it inserted it for me when I added the BindingSources/Adapters/DataSets. I'll have to try recreating it to see exactly what happens. Not as easy as Access, but I'm having fun learning this :)
Jun 24 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.