Below is my code to display device usage by department and employee using treeview control .
Option Compare Database
Dim rec As New ADODB.Recordset
Dim recPlant As New ADODB.Recordset
Dim nodindex As Node
Dim keyDepa, strDepa
Dim keyEm, strEm
Dim keyPl, strPl
Private Sub Form_Load()
AutoMxID
'Set the first level "department"
rec.Open "qry Department_Ascending", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For I = 0 To rec.RecordCount - 1
Set nodindex = Treeview.Nodes.Add(, , "Department" & rec.Fields("depaID"), rec.Fields("depa"), "k1", "k2")
rec.MoveNext
Next
rec.Close
nodindex.Sorted = False
'Set the second level "employee"
'* ---------------------------
rec.Open "qry员工_升序", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For I = 0 To rec.RecordCount - 1
Set nodindex = Treeview.Nodes.Add("部门" & rec.Fields("depaID"), tvwChild, "员工" & rec.Fields("emID"), rec.Fields("emName"), "k1", "k2")
recPlant.Open "Select tblplant.plantid, tblplant.plant, tblplant.emid" & " FROM qry员工使用的设备" & " Where tblplant.emid = '" & rec.Fields("emID") & "'", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For N = 0 To recPlant.RecordCount - 1
Set nodindex = Treeview.Nodes.Add("员工" & recPlant.Fields("emID"), tvwChild, "设备" & recPlant.Fields("PlantID"), recPlant.Fields("Plant"), "k1", "k2")
nodindex.Sorted = True
recPlant.MoveNext
Next N
recPlant.Close
nodindex.Sorted = True
rec.MoveNext
Next I
rec.Close
End Sub
Private Sub Treeview_NodeClick(ByVal Node As Object)
Dim nodindex As Node
Click event is NodeClick
If Node.Key Like "Department*" Then
keyDepa = Mid(Node.Key, 3)
strDepa = Node.Text: Me.Declare Department = strDepa
End If
If Node.Key Like "Employee*" Then
keyEm = Mid(Node.Key, 3)
strEm = Node.Text: Me.Declare Employee = strEm
End If
If Node.Key Like "Device*" Then
keyPl = Mid(Node.Key, 3)
strPl = Node.Text: Me.DeviceName = strPl:Me.DeviceNumber = keyPl
End If
End Sub