Hi, I'm trying to set a relationship between three combo boxes on my main form that's being populated from a database. At first I had my database information setup all in one table like this.
Attachment 99263
But I read a thread from jmcilhinney that I should separate the tables and set a relationship between them. So then I redid the database with Make and Model as their separate tables and the vehicle table has the remaining information inside(Should "Year" have it's own table since i have 3 combo boxes?)
Attachment 99265
Now considering I am connecting to a database instead of making my own tables, how can I modify this code to work on my behalf?
Would I even need this Functions?
GetDataSet()
GetParentTable()
GetChildTable()
GetGrandChildTable()
jmcilhinney's code from Parent/Child Data-binding thread
Current Code just removes the duplicated but does not bind the relationships between each combo boxes
Attachment 99263
But I read a thread from jmcilhinney that I should separate the tables and set a relationship between them. So then I redid the database with Make and Model as their separate tables and the vehicle table has the remaining information inside(Should "Year" have it's own table since i have 3 combo boxes?)
Attachment 99265
Now considering I am connecting to a database instead of making my own tables, how can I modify this code to work on my behalf?
Would I even need this Functions?
GetDataSet()
GetParentTable()
GetChildTable()
GetGrandChildTable()
jmcilhinney's code from Parent/Child Data-binding thread
Code:
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 DataSet = Me.GetDataSet()
'Bind the parent source to the parent table.
Me.BindingSource1.DataSource = data
Me.BindingSource1.DataMember = "Parent"
'Bind the child source to the parent/child relationship.
Me.BindingSource2.DataSource = Me.BindingSource1
Me.BindingSource2.DataMember = "ParentChild"
'Bind the child source to the child/grandchild relationship.
Me.BindingSource3.DataSource = Me.BindingSource2
Me.BindingSource3.DataMember = "ChildGrandchild"
'Bind the parent control to the parent source.
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"
Me.ComboBox1.DataSource = Me.BindingSource1
'Bind the child control to the child source.
Me.ComboBox2.DisplayMember = "Name"
Me.ComboBox2.ValueMember = "ID"
Me.ComboBox2.DataSource = Me.BindingSource2
'Bind the grandchild control to the grandchild source.
Me.ComboBox3.DisplayMember = "Name"
Me.ComboBox3.ValueMember = "ID"
Me.ComboBox3.DataSource = Me.BindingSource3
End Sub
Private Function GetDataSet() As DataSet
Dim data As New DataSet
Dim parent As DataTable = Me.GetParentTable()
Dim child As DataTable = Me.GetChildTable()
Dim grandchild As DataTable = Me.GetGrandChildTable()
data.Tables.Add(parent)
data.Tables.Add(child)
data.Tables.Add(grandchild)
'Add a relationship between the ID of the parent
'table and the ParentID of the child table.
data.Relations.Add("ParentChild", _
parent.Columns("ID"), _
child.Columns("ParentID"))
'Add a relationship between the ID of the child
'table and the ChildID of the grandchild table.
data.Relations.Add("ChildGrandchild", _
child.Columns("ID"), _
grandchild.Columns("ChildID"))
Return data
End Function
Private Function GetParentTable() As DataTable
Dim table As New DataTable("Parent")
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 DataTable
Dim table As New DataTable("Child")
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
Private Function GetGrandChildTable() As DataTable
Dim table As New DataTable("Grandchild")
With table.Columns
.Add("ID", GetType(Integer))
.Add("ChildID", GetType(Integer))
.Add("Name", GetType(String))
End With
With table.Rows
.Add(1, 1, "Grandchild1")
.Add(2, 1, "Grandchild2")
.Add(3, 1, "Grandchild3")
.Add(4, 2, "Grandchild4")
.Add(5, 2, "Grandchild5")
.Add(6, 2, "Grandchild6")
.Add(7, 3, "Grandchild7")
.Add(8, 3, "Grandchild8")
.Add(9, 3, "Grandchild9")
.Add(10, 4, "Grandchild10")
.Add(11, 4, "Grandchild11")
.Add(12, 4, "Grandchild12")
.Add(13, 5, "Grandchild13")
.Add(14, 5, "Grandchild14")
.Add(15, 5, "Grandchild15")
.Add(16, 6, "Grandchild16")
.Add(17, 6, "Grandchild17")
.Add(18, 6, "Grandchild18")
.Add(19, 7, "Grandchild19")
.Add(20, 7, "Grandchild20")
.Add(21, 7, "Grandchild21")
.Add(22, 8, "Grandchild22")
.Add(23, 8, "Grandchild23")
.Add(24, 8, "Grandchild24")
.Add(25, 9, "Grandchild25")
.Add(26, 9, "Grandchild26")
.Add(27, 9, "Grandchild27")
End With
Return table
End Function
End Class
Current Code just removes the duplicated but does not bind the relationships between each combo boxes
Code:
Private Sub LoadData()
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=A_World.accdb;Persist Security Info=False;")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT Distinct Make, Model, Year FROM tblAuto GROUP BY Make, Model, Year ORDER by Make, Model, Year", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "tblAuto")
Dim distinctDT As DataTable = myDataSet.Tables("tblAuto").DefaultView.ToTable(True, "Make")
cboMake.DataSource = distinctDT
cboMake.ValueMember = "Make"
cboMake.DisplayMember = "Make"
cboModel.DataSource = myDataSet.Tables("tblAuto").DefaultView.ToTable(True, "Model")
cboModel.ValueMember = "Model"
cboModel.DisplayMember = "Model"
cboYear.DataSource = myDataSet.Tables("tblAuto").DefaultView.ToTable(True, "Year")
cboYear.ValueMember = "Year"
cboYear.DisplayMember = "Year"
con.Close()
con = Nothing
End Sub
End Class