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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

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

    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.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    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.UserT ypeId value).



    ' Load the data.
    daUserTypes.Fil l(dsUserData)
    daUsers.Fill(ds UserData)



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


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

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      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.

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        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 = TblEmployeeBind ingSource - EmployeeType
        This is the foreign key field

        DataSource = TblEmployeeType BindingSource
        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.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          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?

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Code:
            'TODO: This line of code loads data into the 'LT_TestDataSet.tblEmployeeType' table. You can move, or remove it, as needed.
                    Me.TblEmployeeTypeTableAdapter.Fill(Me.LT_TestDataSet.tblEmployeeType)
                    'TODO: This line of code loads data into the 'LT_TestDataSet.tblEmployee' table. You can move, or remove it, as needed.
                    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 :)

            Comment

            Working...