This is the code I ended up using, when I hard coded it :o :
With tvwCode
.Nodes.Add , , "Source", "Source Code", "Folder Closed"
Do While Not rs.EOF
'check if node is same or different
If strNodeParent <> rs.Fields("tvwNodes").Value Then
'if different, it's a new parent node
strNodeParent = rs.Fields("tvwNodes").Value
'add 1 to the key
lngNodeParent = lngNodeParent + 1
'update key number if previous Parent node had children
If lngNodeParent <= lngNodeChild Then lngNodeParent = lngNodeChild + 1
'add the new node, with the new key and it's name
.Nodes.Add "Source", tvwChild, "P" & CStr(lngNodeParent), strNodeParent, "Folder Closed"
If PCtr = 0 Then
strParent(PCtr) = strNodeParent
NodesKey(KeyCtr) = strNodeParent & " P" & CStr(lngNodeParent)
PCtr = PCtr + 1
KeyCtr = KeyCtr + 1
Else
ReDim Preserve strParent(UBound(strParent) + 1)
strParent(PCtr) = strNodeParent
ReDim Preserve NodesKey(UBound(NodesKey) + 1)
NodesKey(KeyCtr) = strNodeParent & " P" & CStr(lngNodeParent)
PCtr = PCtr + 1
KeyCtr = KeyCtr + 1
End If
End If
'if needed, add the child
'check if there are any child nodes -
'if Fields is "" then no new child nodes
'move on to next parent node
Select Case rs!tvwnodes
Case Is = "API", "Modules", "SQL"
If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
'if Fields is not empty - add new child node
strNodeChild = rs.Fields("tvwChildNodes").Value & ""
If strNodeChild <> "" Then '(dont add Null/blank child nodes)
'add 1 to the key
lngNodeChild = lngNodeChild + 1
'if necessary add 1 to make number unique
If lngNodeChild <= lngNodeParent Then lngNodeChild = lngNodeParent + 1
'add the new child node with the path of the parent node
.Nodes.Add "P" & CStr(lngNodeParent), tvwChild, "C" & CStr(lngNodeChild), strNodeChild, "Leaf"
If CCtr = 0 Then
strChild(CCtr) = strNodeParent & " " & strNodeChild
If KeyCtr > 0 Then
ReDim Preserve NodesKey(UBound(NodesKey) + 1)
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
KeyCtr = KeyCtr + 1
Else
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
KeyCtr = KeyCtr + 1
End If
CCtr = CCtr + 1
Else
ReDim Preserve strChild(UBound(strChild) + 1)
strChild(CCtr) = strNodeParent & " " & strNodeChild
ReDim Preserve NodesKey(UBound(NodesKey) + 1)
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
CCtr = CCtr + 1
KeyCtr = KeyCtr + 1
End If
End If
End If
Case Else
If strNodeChild <> rs.Fields("tvwChildNodes").Value & "" Then
'if Fields is not empty - add new child node
strNodeChild = rs.Fields("tvwChildNodes").Value & ""
If strNodeChild <> "" Then '(dont add Null/blank child nodes)
'add 1 to the key
lngNodeChild = lngNodeChild + 1
'if necessary add 1 to make number unique
If lngNodeChild <= lngNodeParent Then lngNodeChild = lngNodeParent + 1
'add the new child node with the path of the parent node
.Nodes.Add "P" & CStr(lngNodeParent), tvwChild, "C" & CStr(lngNodeChild), strNodeChild, "Folder Closed"
If CCtr = 0 Then
strChild(CCtr) = strNodeParent & " " & strNodeChild
If KeyCtr > 0 Then
ReDim Preserve NodesKey(UBound(NodesKey) + 1)
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
KeyCtr = KeyCtr + 1
Else
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
KeyCtr = KeyCtr + 1
End If
CCtr = CCtr + 1
Else
ReDim Preserve strChild(UBound(strChild) + 1)
strChild(CCtr) = strNodeParent & " " & strNodeChild
ReDim Preserve NodesKey(UBound(NodesKey) + 1)
NodesKey(KeyCtr) = strNodeParent & " " & strNodeChild & " " & "C" & CStr(lngNodeChild)
CCtr = CCtr + 1
KeyCtr = KeyCtr + 1
End If
End If
End If
End Select
'(grand-child is similar to child, but you dont need to store the text/key!)
If Not IsNull(rs.Fields("tvwGrandChildNodes")) Then
If strNodeGrandChild <> rs.Fields("tvwGrandChildNodes").Value & "" Then
'if Fields is not empty - add new grandchild node
strNodeGrandChild = rs.Fields("tvwGrandChildNodes").Value & ""
.Nodes.Add "C" & CStr(lngNodeChild), tvwChild, , strNodeGrandChild, "Leaf"
End If
End If
rs.MoveNext
Loop
End With