tmreiter
asked on
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.
Can anyone explain what I'm doing wrong?
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
Do
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
When I tried replacing the line: treTree.Nodes.Add nodNodeP, tvwChild, nodNode.Key, nodNode.Text
with:Set nodNode = Form_frmCreateUnit!treUnitPurch.Nodes.Add(nodNodeP, tvwChild, nodNode.Key, nodNode.Text)
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?
ASKER
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:
Is it correct to declare the node variables as "MSComctlLib.node"?
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
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"?
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:
http://www.jkp-ads.com/Articles/treeview.asp
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.
That said, you might consider using this Treeview instead:
http://www.jkp-ads.com/Articles/treeview.asp
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.
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
treTree.Nodes.Add nodNodeP, tvwChild, nodNode.Key, nodNode.Text
Are you sure that value is unique in the NEW tree?