Link to home
Start Free TrialLog in
Avatar of Data-Man
Data-ManFlag for United States of America

asked on

Populate Treeview Not Using Recursive Logic

Hello everyone,

    I have some data (recordset) that looks like this...there are a number of other columns that have descriptions and totals for the job, phase and task, however, they are just filler columns.  The 3 columns that I would like the tree built on are listed.


Job #       Phase  Task  
----------- -----     -----
IMP S       NULL   NULL  
IMP1        NULL   NULL
IMP3        00000 00010
IMP3        00000 00020      
IMP3        00000 00030
IMP3        00000 00040
IMP3        00000 00050        
IMP3        00000 00060
IMP3        00000 00127    
IMP3        00000 00180
IMP3        00000 00499
IMP3        00500 00520      
IMP3        00500 00750
IMP3        00500 00970
IMP3        00500 00990
IMP3        01000 01010    
IMP3        01000 01070        
IMP9       NULL  NULL

    How do I put this into a tree view.  Notice there is no recusive logic in this table.  I've populated another tree view control using recusive logic code which calls a procedure until the tree is populated.  That seems simple compared to this...Not sure what I'm missing.

    I can't seem to figure this one out.  I've been looking for documentation on the tree view, but the help file is no where to be found.  

    I was using this code from rockiroads that was posted a while back, but his only had two levels and not three.  

Thanks in advance,
Mike



Avatar of flavo
flavo
Flag of Australia image

Avatar of Data-Man

ASKER

I was looking for something a little more difinitive.  I was able to get it to work, but there has to be an easier way.  Here is my code.

    Me.tvStructure.Nodes.Clear
   
    'Build the Root Nodes
    Do While Not rstTemp.EOF
   
        'If any of the name changes then we have a new parent
        If rstTemp("Job #") <> strRootNode Then
            Me.tvStructure.Nodes.Add , , "Job # " & rstTemp("Job #"), rstTemp("Job #") & " " & rstTemp("Job Name") & " " & rstTemp("Job Total") & " " & rstTemp("Township Total")
        End If
       
        strRootNode = rstTemp("Job #")
   
        rstTemp.MoveNext
    Loop

    rstTemp.MoveFirst
    strRootNode = ""
   
    'Build the First Level Nodes
    Do While Not rstTemp.EOF
   
        'If any of the name changes then we have a new parent
        If (rstTemp("Job #") <> strRootNode Or rstTemp("Phase") <> strFirstNode) And (IsNull(rstTemp("Phase")) = False And rstTemp("Phase") <> strFirstNode) Then
            Me.tvStructure.Nodes.Add "Job # " & rstTemp("Job #"), tvwChild, "Phase " & rstTemp("Phase"), rstTemp("Phase") & " " & rstTemp("Phase Description") & " " & rstTemp("Phase Total") & " " & rstTemp("Township Phase Total")
        End If
       
        strFirstNode = Nz(rstTemp("Phase"), "None")
        strRootNode = rstTemp("Job #")
       
        rstTemp.MoveNext
    Loop
       
    rstTemp.MoveFirst
    strRootNode = ""
    strFirstNode = ""

    'Build the Second Level Nodes
    Do While Not rstTemp.EOF

        'If any of the name changes then we have a new parent
        If (rstTemp("Job #") <> strRootNode Or rstTemp("Task") <> strFirstNode) And (IsNull(rstTemp("Task")) = False And rstTemp("Task") <> strSecondNode) Then
            Me.tvStructure.Nodes.Add "Phase " & rstTemp("Phase"), tvwChild, "Task " & rstTemp("Task"), rstTemp("Task") & " " & rstTemp("Task Description") & " " & rstTemp("Task Total") & " " & rstTemp("Township Task Total")
        End If

        strSecondNode = Nz(rstTemp("Task"), "None")
        strFirstNode = Nz(rstTemp("Phase"), "None")
        strRootNode = rstTemp("Job #")

        rstTemp.MoveNext
    Loop
hmm..

pseudo code

make sure your rs is odered by Job # and Phase  



while not rs.eof

      sRoot = rs("job")
      Add parent node
         while sRoot = rs("job")
             Add first child
                sChile = rs("phase")
                     while rs("phase") = sChild
                           Add second child
                           rs.movenext
                      wend
                      rs.movenext
                 wend
              rs.movenext
        wend

just one loop

????
I was thinking along those lines, but I couldn't sus out the logic...so I broke it down into 3 groups.  Maybe my brain is wired funny...but the more complicated and convoluted the solution...the essier it is for me to understand....LOL

Mike
i think you'd also need

 Comment from flavo
Date: 11/24/2004 08:13AM EST
ID: 12660098
 Your Comment  


Mike,

Seen the MSDN help?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusingtreeviewcontrol.asp
&
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusingtreeviewcontrol.asp
 
Comment from Data-Man
Date: 11/24/2004 08:21AM EST
ID: 12660154
 Author Comment  


I was looking for something a little more difinitive.  I was able to get it to work, but there has to be an easier way.  Here is my code.

    Me.tvStructure.Nodes.Clear
   
    'Build the Root Nodes
    Do While Not rstTemp.EOF
   
        'If any of the name changes then we have a new parent
        If rstTemp("Job #") <> strRootNode Then
            Me.tvStructure.Nodes.Add , , "Job # " & rstTemp("Job #"), rstTemp("Job #") & " " & rstTemp("Job Name") & " " & rstTemp("Job Total") & " " & rstTemp("Township Total")
        End If
       
        strRootNode = rstTemp("Job #")
   
        rstTemp.MoveNext
    Loop

    rstTemp.MoveFirst
    strRootNode = ""
   
    'Build the First Level Nodes
    Do While Not rstTemp.EOF
   
        'If any of the name changes then we have a new parent
        If (rstTemp("Job #") <> strRootNode Or rstTemp("Phase") <> strFirstNode) And (IsNull(rstTemp("Phase")) = False And rstTemp("Phase") <> strFirstNode) Then
            Me.tvStructure.Nodes.Add "Job # " & rstTemp("Job #"), tvwChild, "Phase " & rstTemp("Phase"), rstTemp("Phase") & " " & rstTemp("Phase Description") & " " & rstTemp("Phase Total") & " " & rstTemp("Township Phase Total")
        End If
       
        strFirstNode = Nz(rstTemp("Phase"), "None")
        strRootNode = rstTemp("Job #")
       
        rstTemp.MoveNext
    Loop
       
    rstTemp.MoveFirst
    strRootNode = ""
    strFirstNode = ""

    'Build the Second Level Nodes
    Do While Not rstTemp.EOF

        'If any of the name changes then we have a new parent
        If (rstTemp("Job #") <> strRootNode Or rstTemp("Task") <> strFirstNode) And (IsNull(rstTemp("Task")) = False And rstTemp("Task") <> strSecondNode) Then
            Me.tvStructure.Nodes.Add "Phase " & rstTemp("Phase"), tvwChild, "Task " & rstTemp("Task"), rstTemp("Task") & " " & rstTemp("Task Description") & " " & rstTemp("Task Total") & " " & rstTemp("Township Task Total")
        End If

        strSecondNode = Nz(rstTemp("Task"), "None")
        strFirstNode = Nz(rstTemp("Phase"), "None")
        strRootNode = rstTemp("Job #")

        rstTemp.MoveNext
    Loop
 
Comment from flavo
Date: 11/24/2004 08:27AM EST
ID: 12660195
 Your Comment  


hmm..

pseudo code

make sure your rs is odered by Job # and Phase  



while not rs.eof

      sRoot = rs("job")
      Add parent node
         while sRoot = rs("job") and not rs.eof
             Add first child
                sChile = rs("phase")
                     while rs("phase") = sChild and not rs.eof
                           Add second child
                           rs.movenext
                      wend
                      rs.movenext
                 wend
              rs.movenext
        wend
 
to stop errors (ie looping too far - past the end of the rs)
ASKER CERTIFIED SOLUTION
Avatar of flavo
flavo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You got it :-)