Create Treeview Nodes based on string

Posted on 2006-03-31
Last Modified: 2008-01-09

I have around 20,000 records in a text file. Each line contains something like the following:

   Microwave Ovens\Samsung\FC139STFC\20050809091926296_1.Precaution.pdf
   Microwave Ovens\LG\MS6682NL\Exploded_view.pdf
   Microwave Ovens\LG\Ms324sce Ms325sce\Parts Lists\MS325SCE.csv

   Each of the above is followed by comma delimited date, time, filesize etc

  I need to create Parent/Child nodes in a Treeview using this info. For example,

     Microwave Ovens
   Then populate a Listview with the filename i.e 20050809091926296_1.Precaution.pdf  

   Does anyone have some sample code that demonstrates this, please.


Question by:tvtech
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    It's fairly straight forward to do this but I don't know how practical it would be as I'm not sure a TreeView can handle that many nodes.  At the very least it would certainly be tedious for a human to have to filter thru that many records...

    Author Comment

    If you have some source code, by all means please post it, as I can always mod the code later to import the text file into a MDB. Humans don't need to filter the data, as a search facility will be added later. The main thing is to make it work.


    LVL 85

    Accepted Solution

    Here's the gist of it...

    Option Explicit

    Private Sub Form_Load()
        On Error Resume Next
        Dim ff As Integer
        Dim fileName As String
        Dim inputLine As String
        Dim values As Variant
        Dim nodes As Variant
        Dim Node As String
        Dim i As Integer
        Dim n As Node
        Dim parentN As Node
        ff = FreeFile
        fileName = "c:\someFile.txt"
        Open fileName For Input As #ff
        While Not EOF(ff)
            Line Input #ff, inputLine
            values = Split(inputLine, ",")
            nodes = Split(values(0), "\")
            Set parentN = Nothing
            For i = 0 To UBound(nodes)
                If i < UBound(nodes) Then
                    Set n = Nothing
                    Set n = TreeView1.nodes(nodes(i))
                    If n Is Nothing Then
                        If parentN Is Nothing Then
                            Set n = TreeView1.nodes.Add(, , nodes(i), nodes(i))
                            Set n = TreeView1.nodes.Add(parentN, tvwChild, nodes(i), nodes(i))
                        End If
                    End If
                    Set parentN = n
                    parentN.Tag = nodes(i)
                End If
            Next i
        Close #ff
    End Sub

    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
        If Node.Tag <> "" Then
            Debug.Print Node.Tag
        End If
    End Sub

    Author Comment

    Idle_Mind, nice work. Many thanks.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now