treeview - i'm on vb6

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

treeview - i'm on vb6

Post by sal21 »

Sorry Hans butI can't find your treeview example anymore...

in other case, i need:

big rooth ITALIA
- First rooth REG-REGIONE
- Second rooth PR-PROVINCIA
- Third rooth ISTAT-COMUNE

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

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

Re: treeview - i'm on vb6

Post by HansV »

Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
19 Jun 2024, 08:12
Perhaps treeview with database access
FOUND!

but i need to join two filed by zona, regione, provincia, comune, similar:

Z-ZONA
REG-RGIONE
PR-PROVINCIA
ISTAT-COMUNE

1-NORD-OVEST
01-PIEMONTE
TO-TORINO
001272-TORINO

my code:

Code: Select all

Option Explicit
Private Sub Form_Load()

    Dim nod As MSComctlLib.Node
    
    Call APRI_CONN
    
    arrLevels = Array("regione", "provincia", "comune", "istatext")
    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
    
End Sub
Sub AddChildren(nod As MSComctlLib.Node, level As Long)

    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim sql As String
    Dim chl As MSComctlLib.Node
    
    fld = arrLevels(level)
    
    sql = "SELECT DISTINCT [" & fld & "] FROM Comuni_ISTAT"
    
    If level > 0 Then
        sql = sql & " WHERE [" & arrLevels(level - 1) & "]='" & Replace(nod.Text, "'", "''") & "'"
    End If
    
    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fld))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    
End Sub


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

Re: treeview - i'm on vb6

Post by HansV »

Does this work?

Code: Select all

Option Explicit

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)

    sql = "SELECT DISTINCT " & fld & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fld))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
19 Jun 2024, 09:51
Does this work?

Code: Select all

Option Explicit

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)

    sql = "SELECT DISTINCT " & fld & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fld))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub

on this line:
Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fld))
You do not have the required permissions to view the files attached to this post.

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

Re: treeview - i'm on vb6

Post by HansV »

Is this better?

Code: Select all

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim fldName As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)
    fldName = "Level" & level

    sql = "SELECT DISTINCT " & fld & " AS Level" & level & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fldName))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
19 Jun 2024, 10:42
Is this better?

Code: Select all

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim fldName As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)
    fldName = "Level" & level

    sql = "SELECT DISTINCT " & fld & " AS Level" & level & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fldName))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
:cheers: :cheers: :cheers: :cheers: :clapping:

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
19 Jun 2024, 10:42
Is this better?

Code: Select all

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim fldName As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)
    fldName = "Level" & level

    sql = "SELECT DISTINCT " & fld & " AS Level" & level & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fldName))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
INSTEAD OF LOOP RST, if I use getrow an array, I get improvements in the speed of filling items?

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

Re: treeview - i'm on vb6

Post by sal21 »

sal21 wrote:
19 Jun 2024, 12:03
HansV wrote:
19 Jun 2024, 10:42
Is this better?

Code: Select all

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim fldName As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)
    fldName = "Level" & level

    sql = "SELECT DISTINCT " & fld & " AS Level" & level & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fldName))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
INSTEAD OF LOOP RST, if I use getrow and array, I get improvements in the speed of filling items?

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

Re: treeview - i'm on vb6

Post by HansV »

The code ran fast on my PC as it is now, but you can try it of course.
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
19 Jun 2024, 10:42
Is this better?

Code: Select all

Private Sub Form_Load()
    Dim nod As MSComctlLib.Node

    Call APRI_CONN

    arrLevels = Array("[Z] & '-' & [ZONA]", "[REG] & '-' & [REGIONE]", _
        "[PR] & '-' & [PROVINCIA]", "[ISTATEXT] & '-' & [COMUNE]")

    With Me.TreeView1
        .LineStyle = tvwRootLines
        .Style = tvwTreelinesPlusMinusText
        .Nodes.Clear
        Set nod = .Nodes.Add(Text:="Italia")
        AddChildren nod, 0
    End With
End Sub

Sub AddChildren(nod As MSComctlLib.Node, level As Long)
    Dim rst As ADODB.Recordset
    Dim fld As String
    Dim fldName As String
    Dim sql As String
    Dim chl As MSComctlLib.Node

    fld = arrLevels(level)
    fldName = "Level" & level

    sql = "SELECT DISTINCT " & fld & " AS Level" & level & " FROM Comuni_ISTAT"

    If level > 0 Then
        sql = sql & " WHERE " & arrLevels(level - 1) & "='" & Replace(nod.Text, "'", "''") & "'"
    End If

    Set rst = New ADODB.Recordset
    rst.Open Source:=sql, ActiveConnection:=CON, Options:=adCmdText
    Do While Not rst.EOF
        Set chl = Me.TreeView1.Nodes.Add(Relative:=nod, Relationship:=tvwChild, Text:=rst(fldName))
        If level < UBound(arrLevels) Then
            AddChildren chl, level + 1
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
no idea...

possible:
ITALIA-(Numbers of zone)
1-PIEMONTE-(Numbers of province)
TO-TORINO-(numbers of comuni)
ecc...

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

Re: treeview - i'm on vb6

Post by HansV »

sal21 wrote:
20 Jun 2024, 10:20
HansV wrote:
19 Jun 2024, 10:42
Is this better?
no idea...
You haven't tested it?
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
20 Jun 2024, 10:30
sal21 wrote:
20 Jun 2024, 10:20
HansV wrote:
19 Jun 2024, 10:42
Is this better?
no idea...
You haven't tested it?
YES! WORK.

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

Re: treeview - i'm on vb6

Post by HansV »

I'd stick with what works now. Adding the count is possible but complicated.
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
20 Jun 2024, 11:05
I'd stick with what works now. Adding the count is possible but complicated.
Ok

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

Re: treeview - i'm on vb6

Post by sal21 »

curious...
How to count the last children item, in my case i need to count all ISTAT-COMUNE

MY TEST
Debug.Print TreeView1.nodes(0).children.Count

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

Re: treeview - i'm on vb6

Post by HansV »

You can open a recordset that counts records of course.
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
20 Jun 2024, 11:05
I'd stick with what works now. Adding the count is possible but complicated.
if i have understand this

AddChildren CHL, LEVEL + 1

add new children in treeview, and once inserted I can modify it?

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

Re: treeview - i'm on vb6

Post by HansV »

You can change the Text property of a node (child) using code.
Best wishes,
Hans

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

Re: treeview - i'm on vb6

Post by sal21 »

HansV wrote:
21 Jun 2024, 17:56
You can change the Text property of a node (child) using code.
???
For example.?