Create Treeview Nodes based on string

Posted on 2006-03-31
Medium Priority
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
  • 2
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16348067
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

ID: 16348136
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 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16348596
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

ID: 16349024
Idle_Mind, nice work. Many thanks.


Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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