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
NOTE:
just set the treeview with:
1 - tvwRootLines and the Style property to 6 - tvwTreelinesPlusMinusText