Avatar of Altaf Khan
Altaf Khan asked on

How to create tree view with sub view

Hi
I am trying to create tree view and so far my progress is as follow..
but not as i expected ..
my code is as follow
    With RsHLList
        Do While Not .EOF
            TreeView1.Style = tvwTreelinesPlusMinusText
            
            With TreeView1.Nodes.Add(, , , RsHLList(0), 1)
                TreeView1.Nodes.Add .Index, tvwChild, , RsHLList(1), 2
                With TreeView1.Nodes.Add(.Index, tvwChild, , RsHLList(2), 2)
                    .Expanded = True
                End With
                .Expanded = True
            End With
            .MoveNext
        Loop
        .Close
    End With

Open in new window


i want to tree view like
ALTAF
    50
              10
              12
              18
ALTAF
     100
            14
            22
            26
            35
JHON
     250
            25
            30
EE_treeview.jpg
Visual Basic Classic

Avatar of undefined
Last Comment
Altaf Khan

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Altaf Khan

thanks for reply..
let me try
ASKER
Altaf Khan

Getting run time error 13 - type mismatch

and highlighted on
               Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
Ryan Chong

Getting run time error 13 - type mismatch

have you declared the object properly?

Dim node_root As Node

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
Altaf Khan

yes .. please check following code



 Dim RsHLList As New ADODB.Recordset

   Dim cnt As Integer, tmpidx As Integer, i As Integer
    
    Dim node_root As Node
    Dim node_2nd As Node
    Dim node_3rd As Node
    Dim key_root As String
    Dim key_2nd As String
    Dim key_3rd As String

Set RsHLList = Nothing
If RsHLList.State = 1 Then RsHLList.Close
        
    RsHLList.Open "SELECT Distinct(EmpName), LoanAMT, RCVD_Amount  FROM HandLoan_DTL where Loan_ON_OFF = 'ON' order by EmpName", Con_Main, adOpenKeyset, adLockOptimistic
    
    
    TreeView1.Style = tvwTreelinesPlusMinusText
    
    With RsHLList
        Do While Not .EOF
            cnt = cnt + 1
            
            If tmpidx = RsHLList(1) Then
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
            Else
                key_root = "root" & cnt
                key_2nd = "node_2nd" & cnt
                key_3rd = "node_3rd" & cnt
                
                Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
                Set node_2nd = TreeView1.Nodes.Add(key_root, tvwChild, key_2nd, RsHLList(1), 1)
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
                
                tmpidx = RsHLList(1)
            End If
            
            .MoveNext
        Loop
        .Close
    End With
    
    For i = 1 To TreeView1.Nodes.Count
        TreeView1.Nodes(i).Expanded = True
    Next
    

Open in new window

ASKER
Altaf Khan

sorry my mistake..
it was image list initializing problem ...
let me check again
Ryan Chong

it worked for me.

Private Sub test2()
    Dim Con_Main As ADODB.Connection
    Dim connstr As String
    
    
    ImageList1.ListImages.Add 1, "key1", LoadPicture("C:\test\tmp1.jpg")
    ImageList1.ListImages.Add 2, "key2", LoadPicture("C:\test\tmp2.jpg")
    ImageList1.ListImages.Add 3, "key3", LoadPicture("C:\test\tmp3.jpg")
    Set Me.TreeView1.ImageList = Me.ImageList1
    
    connstr = "Provider=MSOLEDBSQL;DataTypeCompatibility=80;Server=ServerName;Database=EE;UID=UserID;PWD=Password;"
    
    Set Con_Main = New ADODB.Connection
    Con_Main.ConnectionString = connstr
    Con_Main.Open
    
    Dim RsHLList As New ADODB.Recordset

    Dim cnt As Integer, tmpidx As Integer, i As Integer
    
    Dim node_root As Node
    Dim node_2nd As Node
    Dim node_3rd As Node
    Dim key_root As String
    Dim key_2nd As String
    Dim key_3rd As String

    Set RsHLList = Nothing
    If RsHLList.State = 1 Then RsHLList.Close
        
    RsHLList.Open "SELECT Distinct(EmpName), LoanAMT, RCVD_Amount  FROM HandLoan_DTL where Loan_ON_OFF = 'ON' order by EmpName", Con_Main, adOpenKeyset, adLockOptimistic
    
    
    TreeView1.Style = tvwTreelinesPlusMinusText
    
    With RsHLList
        Do While Not .EOF
            cnt = cnt + 1
            
            If tmpidx = RsHLList(1) Then
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
            Else
                key_root = "root" & cnt
                key_2nd = "node_2nd" & cnt
                key_3rd = "node_3rd" & cnt
                
                Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
                Set node_2nd = TreeView1.Nodes.Add(key_root, tvwChild, key_2nd, RsHLList(1), 1)
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
                
                tmpidx = RsHLList(1)
            End If
            
            .MoveNext
        Loop
        .Close
    End With
    
    For i = 1 To TreeView1.Nodes.Count
        TreeView1.Nodes(i).Expanded = True
    Next
    
    Set RsHLList = Nothing
    Con_Main.Close
    Set Con_Main = Nothing
End Sub

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Altaf Khan

Thanks Ryan Chong