A family of Microsoft relational database management systems designed for ease of use.
Because we now want to fill the treeview when the form is loaded and also when an item is selected in the combo box, I split off the routine to fill the treeview:
Private Sub FillTreeView()
Dim dbs As DAO.Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim rst3 As DAO.Recordset
Dim nod1 As MSComctlLib.Node
Dim nod2 As MSComctlLib.Node
Dim strSQL As String
Me.TreeView0.Nodes.Clear
Set dbs = CurrentDb
strSQL = "SELECT DISTINCT StartDate FROM tblClasses"
If Not IsNull(Me.cboDate) Then
strSQL = strSQL & " WHERE StartDate=#" & Format(Me.cboDate, "mm/dd/yyyy") & "#"
End If
Set rst1 = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
Do While Not rst1.EOF
Set nod1 = Me.TreeView0.Nodes.Add(Key:="D" & rst1!StartDate, _
Text:="Date " & rst1!StartDate)
Set rst2 = dbs.OpenRecordset("SELECT ClassID, ClassNumber FROM tblClasses WHERE StartDate=#" & _
Format(rst1!StartDate, "mm/dd/yyyy") & "# ORDER BY ClassID", dbOpenForwardOnly)
Do While Not rst2.EOF
Set nod2 = Me.TreeView0.Nodes.Add(Relative:=nod1, Relationship:=tvwChild, _
Key:="C" & rst2!ClassID, _
Text:="Class " & rst2!ClassNumber)
Set rst3 = dbs.OpenRecordset("SELECT StudentID, StudentFullName FROM tblStudents WHERE StudentClassID=" & _
rst2!ClassID & " ORDER BY StudentFullName", dbOpenForwardOnly)
Do While Not rst3.EOF
Me.TreeView0.Nodes.Add Relative:=nod2, Relationship:=tvwChild, _
Key:="M" & rst3!StudentID, Text:=rst3!StudentFullName
rst3.MoveNext
Loop
rst3.Close
rst2.MoveNext
Loop
rst2.Close
rst1.MoveNext
Loop
' Optional - expand treeview
For Each nod1 In Me.TreeView0.Nodes
nod1.Expanded = True
Next nod1
Set rst3 = Nothing
Set rst2 = Nothing
rst1.Close
Set rst1 = Nothing
Set dbs = Nothing
End Sub
We can then call it where needed:
Private Sub cboDate_AfterUpdate()
FillTreeView
End Sub
Private Sub Form_Load()
FillTreeView
End Sub
where cboDate is the name of the combo box. With the above code, you don't have to use a query for the treeview - the filtering is done within the code.
The sample database at https://skydrive.live.com/#!/?cid=0cae9ff0a56dd9d4&sc=documents&uc=1&id=CAE9FF0A56DD9D4%21112 has been updated.