Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Save TreeView Structure and Load into Another treeview (HELP!)

Posted on 2009-04-18
18
Medium Priority
?
624 Views
Last Modified: 2012-05-06
Hi,

I just finished populating my treeview control in VBA. How can I save the entire treeview and load it from one userform to another userform by specifially unloading the first userform and loading the second userform?

I tried placing my treeview into a collection as an item but for some reason, if I can't seem to copy the item from the collection to the new treeview.

Is there a function out there that allows me to pass in a collection item and a treeview and allow for the loading to happen for me? This is due in literally 2 days from now and I'm at work on the weekend banging my brain out on this. UGH!

Help with some code please!
0
Comment
Question by:piyushdabomb
  • 8
  • 7
  • 2
17 Comments
 
LVL 6

Expert Comment

by:kalkul
ID: 24178813
Hi,

You can use save & open nodes procedures to save nodes to TMP worksheet and then open from TMP sheet.
I asume that node is named TreeView1, and you have worksheet TMP

Regards, Kalkul
Sub SaveNodes()
Dim Nod As node, tmpRow As Long
  tmpRow = 1
  With ThisWorkbook.Worksheets("tmp")
    .Cells.Clear
    For Each Nod In TreeView1.Nodes
      .Cells(tmpRow, 1).Value = "'" & Nod.Text
      .Cells(tmpRow, 2).Value = "'" & Nod.Key
      If Not Nod.Parent Is Nothing Then
        .Cells(tmpRow, 3).Value = "'" & Nod.Parent
      End If
      tmpRow = tmpRow + 1
    Next
  End With
End Sub
 
Sub OpenNodes()
Dim Nod As node, tmpRow As Long
  tmpRow = 1
  With TreeView1.Nodes
    .Clear
    While Len(ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 1).Value) > 0
      If Len(ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 3).Value) > 0 Then
         .Add relative:=ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 3).Value, relationship:=tvwChild, _
              Key:=ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 2).Value, Text:=ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 1).Value
       Else
         .Add Key:=ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 2).Value, Text:=ThisWorkbook.Worksheets("tmp").Cells(tmpRow, 1).Value
       End If
       tmpRow = tmpRow + 1
    Wend
  End With
End Sub

Open in new window

0
 

Author Comment

by:piyushdabomb
ID: 24180064
Can you show me how you would use these in with a sample workbook. I noticed you used a worksheet tmp to save the nodes. I would like to save the nodes into a collection and somehow index them because I have over 15 treevies controls I am using.
Please provide some examples.  
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24183251
Can I ask why you need the same treeview copied from one form to another? Why not simply refer back to the original form?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:kalkul
ID: 24183266
Hi,

How do you initialy create Treview on first form? Is it dynamical? What information you want to sent ot other form? Also how they are opend?
Cany ou provide example?

Regards, Kalkul
0
 

Author Comment

by:piyushdabomb
ID: 24183916
The same yserform is being used over and over again with new values. Think of each userform as a stage where the user populated information for each stage.


Given that the number of stages is user dependent, I will not know or 'hardcode' the trees. I would love to save each trees values by the stage number it refers to and so if The user decides to go to that stage, I can initialze the tree with values saved from when the user was last modifyiing the tree.

Thoughts?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24183987
Can you not load multiple instances of the form and switch to whichever one the user wants?
Depending on where the data comes from, I'm not sure there will be much improvement between simply rerunning the code to populate the treeview and looping through all the nodes stored in a collection to populate the treeview.
0
 

Author Comment

by:piyushdabomb
ID: 24184415
I don't get it. Here is the situation I'm in:

Userform #1 loads up and asks the user how many stages he has to generate a template for. Then Userform #2 loads and provides the user with information they select from listboxes and drop down boxes. As the user selects these items and clicks "Add to List", the Tree updates accordingly. The user then has the option to click "Next Stage", "Previous Stage" or "Generate Template". If the user clicks "Next Stage", the same Userform #2 loads up identical to Stage #1 and the user selects information from listboxes and ddl's and clicks "Add to List", etc...

Now, if the user clicks "Previous Stage", I want to allow the user to see the results they already pre-selected from Stage #1 so that they can continue working on it. If I don't somehow save the tree from Stage #1, then when the initialization occurs, the user will not be able to see anything but a blank tree.

In essense, all I'm trying to accomplish is saving the current stage data for referral if the user clicks "Previous Stage" or "Generate Template". This is why I want to save the tree so that I can reload it.

You mention "multiple instances" of the form. How can I do that and ensure that the same control TreeView1 is being treated as a multiple instance? Can you clarify that for me? I'm all about performance only if you can show me how to make this happen. Let me know your thoughts!
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24184761
From what you say, I would actually use one form with a multipage control on it and use Previous and Next buttons to move through the pages.
You can also use multiple copies of a form using code like the code below - each time you runt the routine, a new copy of Userform1 will be created and added to the collection:
 

Dim colForms As New collection
 
Sub LoadForms()
   dim frm as Userform1
   set frm = New Userform1
   colForms.Add frm
   frm.show
End Sub

Open in new window

0
 

Author Comment

by:piyushdabomb
ID: 24185129
A multipage userform won't work for, lets say 20 stages and the whole tabbed concept just looks ugly (in my opinion).

If I choose to go about employing your second method, how would I do this exactly? Even if I load all the forms into a collection, do I save the current stage form into the collection, hide it, and then show the next one?

What about during the Userform_Initialize procedure? Do I not reinitialize everything? Thoughts?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24185256
I don't see why a multipage won't work for 20 stages, and I'm not suggesting you have the tabs visible. You set it up like a wizard so that the next and previous buttons are the only way of moving through the tabs.
Each instance of the form is separate. You don't even necessarily need a collection if you add a property to the userform for Parent and Child and then assign the preceding form as the parent when you create the next stage (and assign the new form as the Child of the parent form). The Next and Previous buttons can then simply hide the current form and show the relevant Parent/Child form.
Regards,
Rory
0
 

Author Comment

by:piyushdabomb
ID: 24185360
Rory,

How about an example?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 24185392
Here's a very rough demo of multiple instances of a form.
 

form-test.xls
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24185396
:)
0
 

Author Comment

by:piyushdabomb
ID: 24186209
There is a problem with your example:

If i type in 10 in the first screen, click next, then type in 20 then click next, then type in 30, click next and then 40 and then click previous back to the 10 and then click "next", I get a run time error.

Try it out and let me know what I'm doing wrong.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24186487
I did say it was a very rough demo!! I will look at it when I have a chance.
0
 

Author Comment

by:piyushdabomb
ID: 24186582
Please do :). I'm struggling...sadly.
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 2000 total points
ID: 24189038
That was careless of me - in the Next button's code, change frm.Show to child.Show and it should be fine.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

810 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