Data, ComboBoxes, & The Selected Value

mkovacs13

Member
Joined
Nov 14, 2011
Messages
6
Location
Toledo, Oh
Programming Experience
1-3
I have been scouring the internet for days trying to get a cascade of dependent comboboxes working, however no luck, so i was hoping that someone here might be able to provide so assistance.

I have an access db with two tables (Type) and (Model)

In Type the fields are: TypeID(Auto # pkey) and type (text)
In the Model Table are the fields: TypeID(Number) , ModelID( Auto # pkey) and Model (Text)

trying to be a simple as possible to make my incompetence clear

i add these two tables as a datasource in VB Express 2010, which makes a dataset labeled "Information", i also ensure that the Type is the Parent table and the Model the child with the "TypeID" as the relation between the tables

I have two comboboxes (cbo1, and cb2)

I can have the databound to the comboboxes but what i really need is the following:

in Cbo1 there are only two options ("Floor" - a TypeID of 1 & "Overhead" a TypeID of 2)

when i select "Floor" - a TypeID of 1, i want Cbo2 to only show Models of the "Floor" type.

am i crazy in thinking this is impossible?!?!

any help would be greatly appreciate
 
I very much appreciate the help. I've been starring at this monitor too long today, so i'll give it a go tomorrow. Seems pretty straightforward though, just replace the dataset with my actual dataset, and the required fields. I appreciate your help again! Cheers!
 
jmcilhinney

Still having a little trouble utlizing the concept you presented in your tutorial, specifically how i gather my information from the database. I'll be clear as i can. I'm actually very embarrased that i can't seem to figure this out, espically considering you have preety much provided me the answer.

i've added my datasource (two tables) Combo's are made.
Datasource = closer
Parent table = CloserType - PK is CloserTypeID
Child Table = CloserModel - CloserTypeID is a field in this table so as to refer back

I guess the confussion for me is do i need to do the table.add's that you have, considering i already have tables which were added with the dataset.



Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
'Get the data. The DataSet must contain a Parent table,
'a Child table and a ParentChild relation between them
Dim data As Closer = Me.GetDataSet()

'Bind the parent source to the parent table.
Me.BindingSource1.DataSource = data
Me.BindingSource1.DataMember = "CloserType"

'Bind the child source to the relationship.
Me.BindingSource2.DataSource = Me.BindingSource1
Me.BindingSource2.DataMember = "CloserType_CloserModel"

'Bind the parent control to the parent source.
Me.ComboBox1.DisplayMember = "CloserType"
Me.ComboBox1.ValueMember = "CloserTypeID"
Me.ComboBox1.DataSource = Me.BindingSource1

'Bind the child control to the child source.
Me.ComboBox2.DisplayMember = "CloserModel"
Me.ComboBox2.ValueMember = "CloserModelID"
Me.ComboBox2.DataSource = Me.BindingSource2
End Sub

Private Function GetDataSet() As Closer
Dim data As Closer
Dim type As Closer = Me.GetParentTable()
Dim model As Closer = Me.GetChildTable()

data.Tables.IndexOf(CloserType)
data.Tables.IndexOf(CloserModel)

'Add a relationship between the ID of the parent
'table and the ParentID of the child table.
data.Relations.Add("CloserType_CloserModel", _
CloserType.Columns("CloserTypeID"), _
CloserModel.Columns("CloserTypeID"))

Return data
End Function
' do i even need these sections, considering i already have datatables??
Private Function GetParentTable() As Closer
Dim closerType As Closer("CloserType")

With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With

With table.Rows
.Add(1, "Parent1")
.Add(2, "Parent2")
.Add(3, "Parent3")
End With

Return table
End Function

Private Function GetChildTable() As Closer
Dim table As New Closer("CloserModel")

With table.Columns
.Add("ID", GetType(Integer))
.Add("ParentID", GetType(Integer))
.Add("Name", GetType(String))
End With

With table.Rows
.Add(1, 1, "Child1")
.Add(2, 1, "Child2")
.Add(3, 1, "Child3")
.Add(4, 2, "Child4")
.Add(5, 2, "Child5")
.Add(6, 2, "Child6")
.Add(7, 3, "Child7")
.Add(8, 3, "Child8")
.Add(9, 3, "Child9")
End With

Return table
End Function
End Class
 
Solved

I not sure this is the best way to do it, but because it took me days to figure it out along with Jim's help i though it might be wise/helpful to explain to other newbies how i bound comboboxes together with a child/parent relationship

New Project
Throw two combo boxes on the sheet
Add your database, and the tables (we'll use Parent and Child as the example)

ParentTable
ParentID
ParentName
1
Jane & John Doe
2
Pam & Will Smith

ChildTable
ChildID
ParentID
ChildName
1
1
Jimmy Smith
2
2
Mike Doe


Now, goto the "Edit Dataset with Designer" and created a Parent / Child relationship between the two tables, using ParentID as the Key and Foreign Key Columns

Now lets make the boxes

for combobox1 - we're going to use Data bound items
DataSource = ParentTable
DisplayMember = ParentName
ValueMember = ParentID

for combobox2 = We bind to the Binding Source (Which is what i kept missing)
DataSource = ParentTable_ChildTable (this is located in the ParentTableBindSource
DisplayMember = ChildName

F5 to run it and we have ComboBox1 with Two Selections (ParentName) which drives ComboBox2 and provides the applicable Child.

Hope this helped!!
 
Back
Top