Urgent Help needed. Child Nodes (Treeview) (1 Viewer)

samo8076

New member
Local time
Today, 01:36
Joined
Mar 26, 2007
Messages
2
HI, iv spent many hours trying to figure out how to loop through the child nodes of a node i have currently selected in a treeview control. For the life of me i cant figure it out.

I thought it would be a case of:

For each ChildNode in SelectedNode.Children
'Do required stuff here
Next

But that doesnt work

Has anybody got a simple example or any help at all really?

Thanks in advance.
 

Wiz47

Learning by inches ...
Local time
Yesterday, 20:36
Joined
Nov 30, 2006
Messages
274
HI, iv spent many hours trying to figure out how to loop through the child nodes of a node i have currently selected in a treeview control. For the life of me i cant figure it out.

I thought it would be a case of:

For each ChildNode in SelectedNode.Children
'Do required stuff here
Next

But that doesnt work

Has anybody got a simple example or any help at all really?

Thanks in advance.


This is the usual way to move through a recordset.

Dim db As dao.Database
Dim MylList As dao.Recordset
Set db = CurrentDb()

Set Mylist = db.OpenRecordset("yourquery")

Do Until MyList.EOF

List of things to do here

MyList.MoveNext

Loop
 

samo8076

New member
Local time
Today, 01:36
Joined
Mar 26, 2007
Messages
2
thanks for the reply, but thats not what i meant. i want to loop through the child nodes of a treeview control
 

Wiz47

Learning by inches ...
Local time
Yesterday, 20:36
Joined
Nov 30, 2006
Messages
274
thanks for the reply, but thats not what i meant. i want to loop through the child nodes of a treeview control

Code:
Dim DB As DAO.Database, RS As DAO.Recordset
    Set DB = CurrentDb
    
    ' Open a Recordset and loop through it to fill the TreeView
    ' control.
    ' The Key value, RS!CustomerID, is naturally alphabetical
    
    Set RS = DB.OpenRecordset("Customers", dbOpenForwardOnly)
    Do Until RS.EOF
       Me!axTreeView.Nodes.Add , , RS!CustomerID, RS!CompanyName
       RS.MoveNext
    Loop
    RS.Close
    Set RS = Nothing
    Set DB = Nothing

End Sub

The method is very similar to what I had listed earlier. If you want to loop through the nodes you have to open the recordset, then do a Movenext, Loop. The below link might help (or at least point you in the right direction)

http://support.microsoft.com/kb/209927
 

MarkK

bit cruncher
Local time
Yesterday, 17:36
Joined
Mar 17, 2004
Messages
8,181
The actual properties and methods available in the mscomctllib.node objects are a little counterintuitive. What you propose is more sensible, and Microsoft does implement a Nodes collection for each node in vb.net. Meanwhile, the activeX teeview controls available in Access need code something like this to traverse the children of a node. You can fairly easily modify this to be recursive and traverse the entire tree. Check out more node properties in your object browser.

Code:
Public Sub TraverseChildren(node As MSComctlLib.node)
'  writes the text of each child node to the debug window
'  or a message if no child nodes exist
   Dim n As MSComctlLib.node
   Dim i As Integer
   
   'using the 'Child' property of the node, get the first child
   Set n = node.Child
   'traverse the children using the 'Children' property of the node
   For i = 1 To node.Children
      'display the text of the child node
      Debug.Print n.Text
      'to affect recursion, uncomment this line
      'TraverseChildren n
      'using the 'Next' property of the current child, get the next child
      Set n = n.Next
   Next i
   'indicate no children
   If i = 0 Then Debug.Print "Node has zero children"

End Sub
 

Users who are viewing this thread

Top Bottom