DUBT on loop nodes

User avatar
sal21
PlatinumLounger
Posts: 4519
Joined: 26 Apr 2010, 17:36

DUBT on loop nodes

Post by sal21 »

Code: Select all

Private Sub CONTA_COMUNI()

    Dim MYNODE As Node
    Dim NRTOT As Long

    For Each MYNODE In Me.TreeView1.nodes
        If MYNODE.children = 0 Then
            NRTOT = NRTOT + 1
        End If
        DoEvents
    Next
    Me.LNRC.Caption = Format(NRTOT, "#,##0")
    DoEvents

End Sub
this code loop only into last node of each child?

i ned to loop all item in CN-CUNEO

see image
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 79561
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: DUBT on loop nodes

Post by HansV »

Your loop is superfluous - the number of child nodes of CN-Cuneo-(247) is 247, I assume. And it is also equal to the Children property of that node.
But you can loop through the child nodes as follows:

Code: Select all

Private Sub CONTA_COMUNI()
    Dim MYNODE As Node
    Dim MYCHILD As Node
    Dim I As Long

    For Each MYNODE In Me.TreeView1.Nodes
        If MYNODE.Text = "CN-CUNEO-(247)" Then
            Set MYCHILD = MYNODE.Child
            For I = 1 To MYNODE.Children
                ' Do something with MYCHILD
                ' ...
                Set MYCHILD = MYCHILD.Next
            Next I
            Exit For
        End If
    Next MYNODE
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4519
Joined: 26 Apr 2010, 17:36

Re: DUBT on loop nodes

Post by sal21 »

HansV wrote:
20 Jul 2024, 11:02
Your loop is superfluous - the number of child nodes of CN-Cuneo-(247) is 247, I assume. And it is also equal to the Children property of that node.
But you can loop through the child nodes as follows:

Code: Select all

Private Sub CONTA_COMUNI()
    Dim MYNODE As Node
    Dim MYCHILD As Node
    Dim I As Long

    For Each MYNODE In Me.TreeView1.Nodes
        If MYNODE.Text = "CN-CUNEO-(247)" Then
            Set MYCHILD = MYNODE.Child
            For I = 1 To MYNODE.Children
                ' Do something with MYCHILD
                ' ...
                Set MYCHILD = MYCHILD.Next
            Next I
            Exit For
        End If
    Next MYNODE
End Sub
tks , but i need to loop in all child of level 5, not only for "CN-CUNEO-(247)"
In other is the last level of treeview

in effecti have:

ITALIA
ZONE
REGIONI
PROVINCE
COMUNI

User avatar
HansV
Administrator
Posts: 79561
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: DUBT on loop nodes

Post by HansV »

Doesn't your original code do that?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4519
Joined: 26 Apr 2010, 17:36

Re: DUBT on loop nodes

Post by sal21 »

HansV wrote:
20 Jul 2024, 14:29
Doesn't your original code do that?
tks, yes