1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| Private Sub GroupTree_LoadALL()
Dim nParent As Node, nChild As Node, nAbove As Node
Dim db As DAO.Database
Dim rstParent As DAO.Recordset
Dim rstChild As DAO.Recordset
Dim strSQL As String
Dim i As Long
Dim blnTblParentExsits As Boolean, blnTblChildExsits As Boolean
On Error GoTo ErrMsg
Me.GroupTree.Nodes.Clear
With Me.GroupTree
.LineStyle = 1 'Root lines
.Style = 7 'TreelinesPlusMinusPictureText
.Nodes.Clear 'Delete any existing nodes
End With
Set db = CurrentDb()
'Loop through levels, except for last level since that will be filled as children of the second-to-last level
For i = 1 To 2
'Check if table/query exists
blnTblParentExsits = IsTableQuery("", strNodeQuery & i)
blnTblChildExsits = IsTableQuery("", strNodeQuery & i + 1)
'Check if parent table exists
If blnTblParentExsits Then
Set rstParent = db.OpenRecordset(strNodeQuery & i, dbOpenDynaset)
'Populate Parents
Do While Not rstParent.EOF
'Add node (if it already exists, will throw up error number 35602)
If i = 1 Then
Set nParent = GroupTree.Nodes.Add(, , "'" & (rstParent.Fields("nodeID")), rstParent.Fields("nodeText"))
'Make top-level node visible
nParent.EnsureVisible
Else
Set nParent = GroupTree.Nodes("'" & rstParent.Fields("nodeID"))
'Debug.Print nParent.Index
'Debug.Print nParent.Key
'Debug.Print nParent.Text
'for i > 1, node will have a parent
'Set nAbove = GroupTree.Nodes("'" & (rstParent.Fields("nodeParentID")))
'Set nParent = GroupTree.Nodes.Add(nAbove, tvwChild, "'" & (rstParent.Fields("nodeID")), rstParent.Fields("nodeText"))
'NO NEED TO ADD, already added in previous For i =., only need to add children after i = 1
End If
'Check that that child table/query exists
If blnTblChildExsits Then
'Set then filter recordset
Set rstChild = db.OpenRecordset(strNodeQuery & i + 1, dbOpenDynaset)
rstChild.Filter = "nodeParentID = " & nParent.Key & "'"
Set rstChild = rstChild.OpenRecordset
'Check if there are child nodes
While Not rstChild.EOF
'Add child nodes
Set nChild = GroupTree.Nodes.Add(nParent, tvwChild, "'" & rstChild.Fields("nodeID"), rstChild.Fields("nodeText"))
'Debug.Print nChild.Index
'Debug.Print nChild.Key
rstChild.MoveNext
Wend
End If
'Next Parent
rstParent.MoveNext
Loop
Else
MsgBox "The query '" & strNodeQuery & i & "' does not exist.", vbCritical, "GroupTree_LoadALL()"
GoTo ExitHere:
End If
Next i
ExitHere:
Exit Sub
ErrMsg:
If Err.Number = 35602 Then
'Duplicate node ID, which will happen
Err.Clear
Resume Next
Else
MsgBox "Error in GroupTree_LoadALL(); (" & Err.Number & " - " & Err.Description & ")", vbCritical
End If
GoTo ExitHere
End Sub |
Partager