Results 1 to 8 of 8

Thread: fill TREEVIEW with ado...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    fill TREEVIEW with ado...

    First experience with treeview...

    in Table1 have this fields:

    BigAgency (Fader)
    Agengy (Son of Fader)
    SubAgengy (Son of Agency)

    How to fil the treeview?

    Tnkx

    Note:
    1) Access Database
    2) I need to use ADO

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: fill TREEVIEW with ado...

    There isn't any single answer because so much depends on your data and the purpose of your TreeView control. I get the feeling you haven't worked with this control before and don't know where to begin.

    As usual, the VB6 documentation has explanations and sample code snippets. It sounds like you are trying to run before learning to walk and want an example closer to your specific needs.

    Here is just one example of how you might approach this. Perhaps it will help you get started:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim ParentName As String
        Dim FieldIndex As Integer
        Dim NodeName As String
        Dim NewNode As Node
    
        With New ADODB.Connection
            .Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
                & "Data Source='.';" _
                & "Extended Properties='Text'"
            With .Execute("data.txt", , adCmdTable)
                Do Until .EOF
                    ParentName = vbNullString
                    For FieldIndex = 0 To .Fields.Count - 1
                        NodeName = ParentName & "|" & .Fields(FieldIndex).Value
                        On Error Resume Next
                        Set NewNode = TreeView1.Nodes.Item(NodeName)
                        If Err Then
                            On Error GoTo 0
                            If Len(ParentName) = 0 Then
                                Set NewNode = TreeView1.Nodes.Add(, _
                                                                  tvwNext, _
                                                                  NodeName, _
                                                                  .Fields(FieldIndex).Value)
                            Else
                                Set NewNode = TreeView1.Nodes.Add(ParentName, _
                                                                  tvwChild, _
                                                                  NodeName, _
                                                                 .Fields(FieldIndex).Value)
                            End If
                            NewNode.Expanded = True
                            NewNode.Sorted = True
                        Else
                            On Error GoTo 0
                        End If
                        ParentName = NodeName
                    Next
                    .MoveNext
                Loop
                .Close
            End With
            .Close
        End With
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            TreeView1.Move 0, 0, ScaleWidth, ScaleHeight
        End If
    End Sub
    
    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
        MsgBox Node.FullPath
    End Sub
    That demo uses the Jet Text IISAM to read a text file as a "database table" but reading an MDB table would be very similar.
    Attached Files Attached Files

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: fill TREEVIEW with ado...

    Note that in the demo above TreeView1.PathSeparator was assigned as "|" at design time to match what is used to build paths in the code.

    It also assumes that the current drive and directory are the Project or EXE directory. You could change those to App.Path before opening the Connection.
    Last edited by dilettante; Aug 20th, 2019 at 02:35 PM.

  4. #4
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    178

    Re: fill TREEVIEW with ado...

    If you are using an Access Database, you can sort the records using Order by
    Now the code to load the tree

    Code:
    Private Sub Form_Load()
        
    Dim BigAgency As String
    Dim Agency As String
    'SubAgency (Son of Agency)
        
        With New ADODB.Connection
            .Open "Provider=MSDASQL.1;DBQ=D:\Users\Gilman\Documents\Database1.accdb;DefaultDir=D:\Users\Gilman\Documents;Driver={Microsoft Access Driver (*.mdb, *.accdb)};DriverId=25;FIL=MS Access;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
            With .Execute("Select BigAgency, Agency, SubAgency From Tabla1 Order by BigAgency, Agency, SubAgency")
                Do Until .EOF
                    If BigAgency <> !BigAgency Then
                        'New BigAgency, add it to tree
                        BigAgency = !BigAgency
                        Me.TreeView1.Nodes.Add , , BigAgency, BigAgency
                        Agency = ""
                    End If
                    If Agency <> BigAgency & "|" & !Agency Then
                        'New agency, add it to tree
                        Agency = BigAgency & "|" & !Agency
                        Me.TreeView1.Nodes.Add BigAgency, tvwChild, Agency, !Agency
                    End If
                    Me.TreeView1.Nodes.Add Agency, tvwChild, Agency & "|" & !SubAgency, !SubAgency
                    .MoveNext
                Loop
                .Close
            End With
            .Close
        End With
    End Sub
    And is not needed the use of the On Error Resume Next sentence

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: fill TREEVIEW with ado...

    Whew! I didn't think anybody was still thunking through the ODBC adapter Provider that way. Welcome to 1996. Scary to think where that code got copy/pasted from, much less the hazard of falling back into the crusty old Jet SQL-89 syntax it imposes.

  6. #6
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: fill TREEVIEW with ado...

    Dilettante,

    I wish to use your code for the treeview.
    I need two variations to your code if you can help me with that if you please.

    1. I have two variables tied to each other. The one called foldernames(i) gives me a folder name for a paired filename called filenames(i). I need to add files to the treeview nodes which have the same folder names as nodes, so that they can show when you expand the particular node.
    2. Many of these foldernames(i) are the same, so I need to check whether I do not already have such a folder node.

    Thanks
    PK

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: fill TREEVIEW with ado...

    There isn't any master pattern that works for every case. Study the TreeView object model and then write the code to implement what you want.

    Another example:

    Code:
    Private Sub Form_Load()
        Dim F As Integer
        Dim LineX As Long
        Dim LineText As String
        Dim DirNames() As String
        Dim FileNames() As String
        Dim DirNode As Node
    
        F = FreeFile(0)
        Open "data.txt" For Input As #F
        LineX = -1
        Do Until EOF(F)
            Line Input #F, LineText
            LineX = LineX + 1
        Loop
        ReDim DirNames(LineX), FileNames(LineX)
        LineX = 0
        Seek #F, 1
        Do Until EOF(F)
            Input #F, DirNames(LineX), FileNames(LineX)
            LineX = LineX + 1
        Loop
        Close #F
    
        'Ok, now we have arrays.
    
        With TreeView1.Nodes
            For LineX = 0 To UBound(FileNames)
                On Error Resume Next
                Set DirNode = .Item(DirNames(LineX))
                If Err Then
                    On Error GoTo 0
                    Set DirNode = .Add(, tvwNext, DirNames(LineX), DirNames(LineX))
                    DirNode.Expanded = True
                    DirNode.Sorted = True
                Else
                    On Error GoTo 0
                End If
                .Add DirNode, _
                     tvwChild, _
                     DirNames(LineX) & "\" & FileNames(LineX), _
                     FileNames(LineX)
            Next
        End With
    End Sub
    Name:  sshot.png
Views: 424
Size:  3.4 KB
    Attached Files Attached Files

  8. #8
    Fanatic Member Peekay's Avatar
    Join Date
    Sep 2006
    Location
    Witbank, South Africa
    Posts
    784

    Re: fill TREEVIEW with ado...

    Wonderful, thank you very much
    PK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width