?
Solved

Treeview - looping through nodes

Posted on 2012-04-01
6
Medium Priority
?
283 Views
Last Modified: 2012-04-02
Hi,

I am simply trying to loop through the nodes of my treeview control with following code:
With TaskItemsForm.trvTasks
        For Each n In trvTasks
            Debug.Print "Key :  ", .Nodes.Item.Key, "Text :   ", .Nodes.Item.Text
        Next n
     End With

Open in new window

but I am getting the error :
Compile error - argument not optional - and it highlights the ".Items" section of .Nodes.Item.Key

Any help on guiding me through this would be appreciated.

Best regards,

Craig
0
Comment
Question by:slobber72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

Accepted Solution

by:
Norie earned 500 total points
ID: 37794250
Craig

Why aren't you using the loop variable n?

Have you checked what it is?

Is it a node?

Try this.
Dim N As Node

        For Each N In trvTasks.Nodes
            Debug.Print "Key :  ", N.Key, "Text :   ", N.Text
        Next N

Open in new window

0
 
LVL 49

Assisted Solution

by:Martin Liss
Martin Liss earned 500 total points
ID: 37794419
Here's code to "Walk" the nodes of a treeview.


Private Sub WalkTree(ByVal ParentNode As Node)
    
    Do Until ParentNode Is Nothing
        Debug.Print ParentNode.Text, ParentNode.Index
        WalkTree ParentNode.Child
        Set ParentNode = ParentNode.Next
    Loop

End Sub

Open in new window


Usage:

WalkTree trvTasks.Nodes(1)
0
 

Author Closing Comment

by:slobber72
ID: 37794479
Thanks guys. I am still a relative novice in coding - the different uses of methods / properties etc still gets me befuddled. You have both given me a way to cycle through the treeview and they both work really well with my code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:slobber72
ID: 37794617
Hi Martin,

I am getting an error when trying to call your procedure- as I am hopeless with VBA in Excel I can't track down what the reason for it is.

When I call the procedure as below, I get the error "Type Mismatch"...

Private Sub UserForm_Initialize()
    With Me
        'Label the userform
        .Caption = "Menu Tasks Comment Form"
        ' Label the buttons
        .CommandButton1.Caption = "Close"
        .CommandButton2.Caption = "Add Comment"
        .CommandButton3.Caption = "View Comments"
        .Frame1.Caption = "Comments"
        'Null out the label
        .Label1 = vbNullString
        .TextBox1 = vbNullString
        'Label the treeview
        .TreeView1.LineStyle = tvwRootLines
        'Sort the treeview
        .TreeView1.Sorted = True
    End With
   'Populate the Treeview
    TreeView_Populate True ' populate the treeview control
    WalkTree Me.TreeView1.Nodes(1)
End Sub

Open in new window


Could you perhaps point me in the right direction as to what I am doing wrong ?

Thanks in advance.
Craig
0
 

Author Comment

by:slobber72
ID: 37794670
Hi again,

I found that I needed to change the procedure to declare the parentnode as an object, not as a node - it would appear that this is a version of treeview issue.

Private Sub WalkTree(ByVal ParentNode As Object) ' previously declared as node
    
    Do Until ParentNode Is Nothing
        Debug.Print ParentNode.Text, , ParentNode.Index, , ParentNode.Key
        WalkTree ParentNode.Child
        Set ParentNode = ParentNode.Next
    Loop

End Sub

Open in new window


Thanks again !!
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 37796587
I'm glad you were able to fix it. Not that I'm complaining but what happened between the time you said my code worked really well and the type mismatch error?

Just out of curiosity, does the following work for you?

Private Sub WalkTree(ByVal ParentNode As ComctlLib.Node)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question