still on treeview, pardon!

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

still on treeview, pardon!

Post by sal21 »

my test code:

Code: Select all

Private Sub APRI_TREEVIEW()

'https://eileenslounge.com/viewtopic.php?f=30&t=36894&p=285948#p285948
'https://eileenslounge.com/viewtopic.php?f=30&t=36898&sid=f121277dab1280c318084470e599b494

    Dim NODMARE As Node
    Dim NODCHILD As Node
    Dim NODSUBCHILD As Node
    Dim I As Long, SQL As String, INDICE As String, INDICE2 As String, TIPO As String

    Set Me.TreeView1.ImageList = Me.ImageList1

    With Me.TreeView1

        Set NODMARE = .Nodes.Add(Key:="REGIONI", Text:="REGIONI", Image:=1)
        Set RS = New ADODB.Recordset
        RS.CursorLocation = adUseClient
        SQL = "SELECT DISTINCT REGIONE FROM CAP"
        RS.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
        RS.Sort = ("REGIONE")

        Do While Not RS.EOF

            INDICE = RS.Fields(0).Value

          ERROR >>>> Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONE", Relationship:=tvwChild, Text:=INDICE, Image:=2)
            SQL = "SELECT DISTINCT PR, PROVINCIA FROM CAP WHERE REGIONE='" & INDICE & "'"

            'Debug.Print SQL
            Set RST = New ADODB.Recordset
            RST.CursorLocation = adUseClient
            RST.Open SQL, CON, adOpenForwardOnly, adLockReadOnly

            Do While Not RST.EOF
                INDICE2 = RST!PR & "-" & RST!PROVINCIA
                Set NODSUBCHILD = .Nodes.Add(Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)
                RST.MoveNext
            Loop

            RST.Close
            Set RST = Nothing

            RS.MoveNext

        Loop

        If Not (RS Is Nothing) Then
            If (RS.State And adStateOpen) = adStateOpen Then RS.Close
            Set RS = Nothing
        End If

        If Not (RST Is Nothing) Then
            If (RST.State And adStateOpen) = adStateOpen Then RST.Close
            Set RST = Nothing
        End If

        Set NODCHILD = Nothing
        Set NODSUBCHILD = Nothing
        Set NODMARE = Nothing

    End With
    
End Sub
started from REGIONE, add PR-PROVINCIA, add CAP-COPMUNE

NOTE:
just set the treeview with:
1 - tvwRootLines and the Style property to 6 - tvwTreelinesPlusMinusText
You do not have the required permissions to view the files attached to this post.

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: still on treeview, pardon!

Post by SpeakEasy »

ERROR >>>> Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONE", Relationship:=tvwChild, Text:=INDICE, Image:=2)

You called the node "REGIONI" not "REGIONE"

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

Re: still on treeview, pardon!

Post by sal21 »

SpeakEasy wrote:
06 Aug 2021, 09:34
ERROR >>>> Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONE", Relationship:=tvwChild, Text:=INDICE, Image:=2)

You called the node "REGIONI" not "REGIONE"
PERFECT!

But now i need to add CAP-COMUNE for each PR-PROVINCIA...

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

Re: still on treeview, pardon!

Post by HansV »

Air code:

Code: Select all

Private Sub APRI_TREEVIEW()

'https://eileenslounge.com/viewtopic.php?f=30&t=36894&p=285948#p285948
'https://eileenslounge.com/viewtopic.php?f=30&t=36898&sid=f121277dab1280c318084470e599b494

    Dim NODMARE As Node
    Dim NODCHILD As Node
    Dim NODSUBCHILD As Node
    Dim I As Long, SQL As String, INDICE As String, INDICE2 As String, TIPO As String
    Dim RS2 As ADODB.Recordset, INDICE3 As String, NODSUBCHILD2 As Node

    Set Me.TreeView1.ImageList = Me.ImageList1

    With Me.TreeView1

        Set NODMARE = .Nodes.Add(Key:="REGIONE", Text:="REGIONE", Image:=1)
        Set RS = New ADODB.Recordset
        RS.CursorLocation = adUseClient
        SQL = "SELECT DISTINCT REGIONE FROM CAP"
        RS.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
        RS.Sort = "REGIONE"

        Do While Not RS.EOF

            INDICE = RS!REGIONE

            Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONE", Relationship:=tvwChild, Text:=INDICE, Image:=2)
            SQL = "SELECT DISTINCT PR, PROVINCIA FROM CAP WHERE REGIONE='" & INDICE & "'"

            'Debug.Print SQL
            Set RST = New ADODB.Recordset
            RST.CursorLocation = adUseClient
            RST.Open SQL, CON, adOpenForwardOnly, adLockReadOnly

            Do While Not RST.EOF
                INDICE2 = RST!PR & "-" & RST!PROVINCIA
                Set NODSUBCHILD = .Nodes.Add(Key:=INDICE2, Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)
                
                SQL = "SELECT CAP, COMUNE FROM CAP WHERE REGIONE='" & RS!REGIONE & "' AND PR='" & RST!PR & "'"
                Set RS2 = New ADODB.Recordset
                RS2.CursorLocation = adUseClient
                RS2.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
                Do While Not RS2.EOF
                    INDICE3 = RS2!CAP & "-" & RS2!COMUNE
                    Set NODSUBCHILD2 = .Nodes.Add(Key:=INDICE3, Relative:=INDICE2, Relationship:=tvwChild, Text:=INDICE3, Image:=6)
                    RS2.MoveNext
                Loop
                RS2.Close
                RST.MoveNext
            Loop

            RST.Close
            Set RST = Nothing

            RS.MoveNext

        Loop

        If Not (RS Is Nothing) Then
            If (RS.State And adStateOpen) = adStateOpen Then RS.Close
            Set RS = Nothing
        End If

        If Not (RST Is Nothing) Then
            If (RST.State And adStateOpen) = adStateOpen Then RST.Close
            Set RST = Nothing
        End If

        If Not (RS2 Is Nothing) Then
            If (RS2.State And adStateOpen) = adStateOpen Then RS2.Close
            Set RS2 = Nothing
        End If
        
        Set NODCHILD = Nothing
        Set NODSUBCHILD = Nothing
        Set NODSUBCHILD2 = Nothing
        Set NODMARE = Nothing

    End With
    
End Sub
Best wishes,
Hans

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

Re: still on treeview, pardon!

Post by sal21 »

HansV wrote:
06 Aug 2021, 10:43
Air code:

Code: Select all

Private Sub APRI_TREEVIEW()

'https://eileenslounge.com/viewtopic.php?f=30&t=36894&p=285948#p285948
'https://eileenslounge.com/viewtopic.php?f=30&t=36898&sid=f121277dab1280c318084470e599b494

    Dim NODMARE As Node
    Dim NODCHILD As Node
    Dim NODSUBCHILD As Node
    Dim I As Long, SQL As String, INDICE As String, INDICE2 As String, TIPO As String
    Dim RS2 As ADODB.Recordset, INDICE3 As String, NODSUBCHILD2 As Node

    Set Me.TreeView1.ImageList = Me.ImageList1

    With Me.TreeView1

        Set NODMARE = .Nodes.Add(Key:="REGIONE", Text:="REGIONE", Image:=1)
        Set RS = New ADODB.Recordset
        RS.CursorLocation = adUseClient
        SQL = "SELECT DISTINCT REGIONE FROM CAP"
        RS.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
        RS.Sort = "REGIONE"

        Do While Not RS.EOF

            INDICE = RS!REGIONE

            Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONE", Relationship:=tvwChild, Text:=INDICE, Image:=2)
            SQL = "SELECT DISTINCT PR, PROVINCIA FROM CAP WHERE REGIONE='" & INDICE & "'"

            'Debug.Print SQL
            Set RST = New ADODB.Recordset
            RST.CursorLocation = adUseClient
            RST.Open SQL, CON, adOpenForwardOnly, adLockReadOnly

            Do While Not RST.EOF
                INDICE2 = RST!PR & "-" & RST!PROVINCIA
                Set NODSUBCHILD = .Nodes.Add(Key:=INDICE2, Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)
                
                SQL = "SELECT CAP, COMUNE FROM CAP WHERE REGIONE='" & RS!REGIONE & "' AND PR='" & RST!PR & "'"
                Set RS2 = New ADODB.Recordset
                RS2.CursorLocation = adUseClient
                RS2.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
                Do While Not RS2.EOF
                    INDICE3 = RS2!CAP & "-" & RS2!COMUNE
                    Set NODSUBCHILD2 = .Nodes.Add(Key:=INDICE3, Relative:=INDICE2, Relationship:=tvwChild, Text:=INDICE3, Image:=6)
                    RS2.MoveNext
                Loop
                RS2.Close
                RST.MoveNext
            Loop

            RST.Close
            Set RST = Nothing

            RS.MoveNext

        Loop

        If Not (RS Is Nothing) Then
            If (RS.State And adStateOpen) = adStateOpen Then RS.Close
            Set RS = Nothing
        End If

        If Not (RST Is Nothing) Then
            If (RST.State And adStateOpen) = adStateOpen Then RST.Close
            Set RST = Nothing
        End If

        If Not (RS2 Is Nothing) Then
            If (RS2.State And adStateOpen) = adStateOpen Then RS2.Close
            Set RS2 = Nothing
        End If
        
        Set NODCHILD = Nothing
        Set NODSUBCHILD = Nothing
        Set NODSUBCHILD2 = Nothing
        Set NODMARE = Nothing

    End With
    
End Sub
TKS for code...

i have modified with array(to pseed up the node fill)... but naturally have error!

Code: Select all

Private Sub APRI_TREEVIEW()

'https://eileenslounge.com/viewtopic.php?f=30&t=36894&p=285948#p285948
'https://eileenslounge.com/viewtopic.php?f=30&t=36898&sid=f121277dab1280c318084470e599b494

    Dim NODMARE As Node
    Dim NODCHILD As Node
    Dim NODCHILDC As Node
    Dim NODSUBCHILD As Node
    Dim I As Long, SQL As String, INDICE As String, INDICE2 As String, INDICE3 As String

    Set Me.TreeView1.ImageList = Me.ImageList1

    With Me.TreeView1

        Set NODMARE = .Nodes.Add(Key:="REGIONI", Text:="REGIONI", Image:=1)
        Set RS = New ADODB.Recordset
        RS.CursorLocation = adUseClient
        SQL = "SELECT DISTINCT NREG, REGIONE FROM CAP"
        RS.Open SQL, CON, adOpenForwardOnly, adLockReadOnly
        RS.Sort = ("REGIONE")

        Do While Not RS.EOF

            INDICE = RS.Fields(0).Value & "-" & RS.Fields(1).Value

            Set NODCHILD = .Nodes.Add(Key:=INDICE, Relative:="REGIONI", Relationship:=tvwChild, Text:=INDICE, Image:=2)
            SQL = "SELECT DISTINCT PR, PROVINCIA FROM CAP WHERE NREG=" & RS.Fields(0).Value & ""

            Set RST = New ADODB.Recordset
            RST.CursorLocation = adUseClient
            RST.Open SQL, CON, adOpenForwardOnly, adLockReadOnly

            Do While Not RST.EOF
            
                INDICE2 = RST!PR & "-" & RST!PROVINCIA
                Set NODSUBCHILD = .Nodes.Add(Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)

                Set RSC = New ADODB.Recordset
                RSC.CursorLocation = adUseClient
                SQL = "SELECT CAP, COMUNE FROM CAP WHERE PR='" & RST!PR & "'"
                RSC.Open Source:=SQL, ActiveConnection:=CON, CursorType:=adOpenForwardOnly, LockType:=adLockReadOnly
                RSC.Sort = ("CAP, COMUNE")

                RSC.MoveFirst
                Erase strDBRows()
                strDBRows = RSC.GetRows()
                RSC.Close
                Set RSC = Nothing

                For C = 0 To UBound(strDBRows, 2)
                    INDICE3 = strDBRows(0, C) & "-" & strDBRows(1, C)
                    Set NODCHILDC = .Nodes.Add(Key:=INDICE3, Relative:=INDICE2, Relationship:=tvwChild, Text:=INDICE3, Image:=5)
                Next C

                RST.MoveNext
                
            Loop

            RST.Close
            Set RST = Nothing

            RS.MoveNext

        Loop

        If Not (RS Is Nothing) Then
            If (RS.State And adStateOpen) = adStateOpen Then RS.Close
            Set RS = Nothing
        End If

        If Not (RST Is Nothing) Then
            If (RST.State And adStateOpen) = adStateOpen Then RST.Close
            Set RST = Nothing
        End If

        Set NODCHILD = Nothing
        Set NODSUBCHILD = Nothing
        Set NODMARE = Nothing

    End With

End Sub

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

Re: still on treeview, pardon!

Post by HansV »

Which line causes the error, and what is the error message?
Best wishes,
Hans

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

Re: still on treeview, pardon!

Post by sal21 »

HansV wrote:
06 Aug 2021, 11:21
Which line causes the error, and what is the error message?
Set NODCHILDC = .Nodes.Add(Key:=INDICE3, Relative:=INDICE2, Relationship:=tvwChild, Text:=INDICE3, Image:=5)

element not found.

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

Re: still on treeview, pardon!

Post by HansV »

Does the imagelist control have an image with index 5?
Best wishes,
Hans

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

Re: still on treeview, pardon!

Post by HansV »

You forgot to set the index for NODSUBCHILD (it is in the code that I posted). The line

Code: Select all

                Set NODSUBCHILD = .Nodes.Add(Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)
should be

Code: Select all

                Set NODSUBCHILD = .Nodes.Add(Key:=INDICE2, Relative:=INDICE, Relationship:=tvwChild, Text:=INDICE2, Image:=4)
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: still on treeview, pardon!

Post by SpeakEasy »

Strikes me you could probably simplify this somewhat by using a hierarchical recordset, i.e using the MSDataShape provider. It would do a lot of the heavy lifting. Trickiest bit is getting familiar with the Data Shaping Service's SHAPE command if it isn't something that you have used before