Copy Node (and Children) from one Treeview to another

Hi, I've got two Treeview controls on a form and want to select a node in TreeView1 and have that selected node and all of its children and grandchildren, etc. copied into Treeview2.  I've got code that correctly recurses through the selected nodes in TreeView1, but am having problems adding the child nodes to Treeview2 (ie treTree).  

In the code below, the results are debug.printing properly and there are no errors, but nothing is added to TreeView2.  Note that the root node for TreeView2 (ie, the selected node in TreeView1) is added via a separate, non-recursive sub which is working properly; that node is passed to TreeViewAddChildren.
Public Sub TreeViewAddChildren(nodNodeP As MSComctlLib.node)
    Dim nodNode As MSComctlLib.node
    Dim treTree As TreeView
    Dim strNodeKey As String
    Set treTree = Form_frmCreateUnit!treUnitPurch.Object

    If nodNodeP.Children = 0 Then Exit Sub
    Set nodNode = nodNodeP.Child
        Debug.Print nodNode.Text & "    " & strNodeKey
        treTree.Nodes.Add nodNodeP, tvwChild, nodNode.Key, nodNode.Text
        TreeViewAddChildren nodNode
        Set nodNode = nodNode.Next
    Loop Until nodNode Is Nothing
End Sub

Open in new window

When I tried replacing the line:
 treTree.Nodes.Add nodNodeP, tvwChild, nodNode.Key, nodNode.Text

Open in new window

Set nodNode = Form_frmCreateUnit!treUnitPurch.Nodes.Add(nodNodeP, tvwChild, nodNode.Key, nodNode.Text)

Open in new window

I got an error ("Runtime error '91'  Object variable or With block variable not set") in Line 8 of the code above.

Can anyone explain what I'm doing wrong?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
It's a little early in the morning for me, but your code seems to be okay. One thing that stands out is your nonNode.Key in this line:

treTree.Nodes.Add nodNodeP, tvwChild, nodNode.Key, nodNode.Text

Are you sure that value is unique in the NEW tree?
tmreiterAuthor Commented:
I always empty the new tree before testing this code, so as far as I understand the key value should be unique in the new tree since it was unique in the old tree.

Just in case, let me post the code which is calling this procedure:
Private Sub cbPurchUnit_Click()
    Dim objNode As MSComctlLib.node
    Set objNode = Me.treUnits.Object.SelectedItem
    Form_frmCreateUnit.treUnitPurch.Nodes.Add , , objNode.Key, objNode.Text
    TreeViewAddChildren objNode
End Sub

Open in new window

The code above (which seems to work correctly) adds the root node to the new treeview and then calls the procedure posted above to add the children and grandchildren.

Is it correct to declare the node variables as "MSComctlLib.node"?
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I believe that's correct, although I've never prefaced it with the MSComctlLib qualifier before - I just used Node.

That said, you might consider using this Treeview instead:

This was created to deal with the myriad issues you'll find when using the MSComctl methods, which don't work well or more recent Access platforms.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

tmreiterAuthor Commented:
I'd seen that Treeview before, but wasn't sure that it was really necessary or how well supported it was.  How difficult do you think it would be to switch what I've got now to the alternate Treeview?
tmreiterAuthor Commented:
For some reason, I could never get my the code posted above to work, so decided to find a different way.  Running the two subs below sequentially in the calling sub works (although note that unlike my original code, it also inserts the original selected node in Tree2).
Public Sub RecurseTree(TopNode As node)
  Dim TempNode As node

  If TopNode.Children > 0 Then
    Set TempNode = TopNode.Child
    Do Until TempNode Is Nothing
      ProcessNode TempNode
      If TempNode.Children > 0 Then RecurseTree TempNode
      Set TempNode = TempNode.Next
  End If
End Sub

Public Sub ProcessNode(TempNode As node)
    Dim treTV As TreeView
    Dim strNodeKey As String
    Dim strNodeDesc As String
    Dim strNodeParent As String
    Dim intNodeCount As Integer

    Set treTV = Form_frmCreateUnit!treUnitPurch.Object
    strNodeKey = TempNode.Key
    strNodeDesc = TempNode.Text
    intNodeCount = treTV.Nodes.Count
    If intNodeCount = 0 Then
        treTV.Nodes.Add , , strNodeKey, strNodeDesc
        strNodeParent = TempNode.Parent.Key
        treTV.Nodes.Add strNodeParent, tvwChild, strNodeKey, strNodeDesc
    End If
End Sub

Open in new window

Along the way, I encountered another strange (and embarrassing) problem:  several times I was not getting any errors, but only the first selected node (and no children, etc.)  appeared in Tree2...  I couldn't figure it out.  When I finally checked Tree2's properties, I noticed that somehow the indent property had been set to 567, with lines turned off.  So, the children WERE being added, but they were being indented far to the right, out of view in the treeview (with no lines indicating that there was anything there.  Fixed those properties and the children magically appeared.  I hadn't made these changes to the properties, so I'm a bit mystified how it happened, but at least now it is working.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I'd seen that Treeview before, but wasn't sure that it was really necessary or how well supported it was.
It was created by several Access and Excel MVPs, so I'd think you wouldn't have much trouble re: support.

As far as "necessary" - the control you're using is well known to cause troubles as you migrate to newer versions of Office and/or Windows, and eventually you'll find that it simply won't work on the platform (and sooner rather than later, I'd suspect). IMO, if you're using the mscomctl library (or any other ActiveX created for use with 32-bit platforms), you should be making plans to remove those and replace them with compliant controls/technology. MSFT is under no obligation to maintain compatibility with those older controls, and they rightly make no effort to insure such. If your application is important to your business, or it's something you've supplied to clients, then it would be in your best interest to either (a) move to a different development platform that supports these sort of UI conventions, like .NET or (b) remove your Access application dependency on non-compliant controls.
tmreiterAuthor Commented:
I don't mean to keep accepting my own comments as solutions, but want to close out this "open question" and when I didn't get any specific responses, I spent several hours finding code that worked (and working out the dumb mistake described above).  Even though Scott's response wasn't particularly useful for solving this problem, I've allocated him 100 pts for taking the time to point out the macro issues that he has.

I've given this a B grade because it doesn't look particularly elegant, in that I've got to use two subs instead of one, as I would have preferred.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.