Combo to display name from second table instead of ID from first table

Ok, here's another noob question. I'm a convert from Access/VBA and learning how to work in Visual Basic.

Problem: Displaying names and not IDs from member table in a combo box that is referred to by its ID in the Program table.

Ok, that needs some more explanation. Here's the setup:

Tables:

  • Program
  • Member
  • Times

Program is related to Member by its foreign key ProgramMemberID (which is MemberID in the Member table). Times is related to Program by its foreign key TimesProgramID (which is ProgramID in the Program table).

So far so good.

Now I create a form with the above mentioned tables and relations as its DataSet. Each form entry is one program, that also has one member selected from the Member table and multiple times in the Times table.

In Access language, this would be a form with the Program table as its source and the subform based on the Times table with records with the same TimesProgramID as the ProgramID of the current program. I can indeed get the times for a particular program to show up fine.

BUT, and here's the issue, in the member combo box I can only see the ProgramMemberID. But I'd like to have the MemberName from the Member table to be what is displayed instead. How do I do that

The binding source I use has the dataset as its source and Program as its member. I suspect this means that it is filtered by the current program, to only display the times etc. for the currenr program. But that also means that I cannot select MemberName from this binding so as the data source when using data bound items in the combo, since the available members for the BindingSource are only what is in the Program table.

Not sure I made myself clear, but I'd be grateful for any advice! The thing is that I want the member in the member combo to change as the program is changing.

Thanks!



Answer this question

Combo to display name from second table instead of ID from first table

  • Zooz

    Hi,

    Just wanted to say that I solved this. After it was solved I also found this help page that explains how to do just what I was looking for: http://msdn2.microsoft.com/en-us/library/ms171924.aspx

    Hope that can help someone else who may be looking for a solution to the same issue. :)


  • Juliano.net

    Hi ahmedilyas,

    Thanks for your reply! I'm not sure where to find the code for the binding part of the combo box in VB 2005 Express Edition. I do the binding by clicking the little arrow at the right in the combo box and ticking "Use data bound items". Then the issue is what binding source to select that includes both the MemberName of the Member table and the ProgramMemberID of the Program table....

    I can only select either one or the other. Do I need to make a DataTable for that, that includes both the Program and Member tables I tried that, but get "ContraintException Unhandled. Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.".

    The code for filling the TableAdapter's looks like this (with the mentioned DataTable):

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.ProgramTableAdapter.Fill(Me.ProgramdatabasDataSet1.Program)
    Me.TimesTableAdapter.Fill(Me.ProgramdatabasDataSet1.Times)
    Me.MemberDataTableTableAdapter.Fill(Me.ProgramdatabasDataSet1._MemberDataTable)

    End Sub

    It's the last .Fill that is causing the above error. The MemberDataTable includes MemberID (key), MemberName and MemberCode from the Member table and ProgramMemberID from the Program table.

    I'm not yet that comfortable with BindingSources and TableAdapters, when having multiple tables in the same form like this.

    Thanks.


  • Michel Paulissen

    are you able to post some code on the binding part of the combobox

    Just to add, in order to show the MemberName, you need to set the DisplayMember property to "MemberName" on the combobox to show the member names from the dataset, and set the ValueMember to "ProgramMemberID"....



  • Combo to display name from second table instead of ID from first table