Remove the selected node in a treeview (A2007)

  • Thread starter Dale_Fye via AccessMonster.com
  • Start date
D

Dale_Fye via AccessMonster.com

I'm trying to remove a node from a treeview, elegantly.

I'm currently deleting the associated record from the underlying table, then
clearing the tree and rebuilding it (not very elegant). I've got the
following code which should allow me to identify the parent node of the
selected node, but it keeps generating a 35602 - Invalid Key error on the
line marked with the <-----

Dim tvw As MSComctlLib.TreeView
Dim nd As MSComctlLib.Node

On Error GoTo DeleteError

If MsgBox("Delete activity", vbCritical + vbYesNo, "") = vbNo Then Exit
Sub

Set tvw = Me.tree_Activities.Object
Set nd = tvw.Nodes(Me.txt_Activity_ID) '<----

When I set a breakpoint on this line, and then go to the Immediate window and
duplicate it, it generates the same error, but when I replace Me.
txt_Activity_ID with the literal "A267" in the immediate window it works
properly, and I can identify the nodes parent, the number of siblings, ...

Anyone have any ideas?

Also, how do I determine the index number of a particular node as it relates
to its parent node. For example, if I have a nodes that look like:

-FY11
|- Item 2
|- Item 1
|- Item 3

What is the best way to determine that Item 1 is the second node belonging to
FY11? I know I could use nd.Next.key and if that generates an error, try nd.
Previous.key, but I was hoping there was a more elegant way.
 
S

Stefan Hoffmann

hi Dale,

Dale_Fye via AccessMonster.com said:
Dim tvw As MSComctlLib.TreeView
Use this:

Option Compare Database
Option Explicit

Private m_TreeView As MSComctlLib.TreeView

Private Sub Form_Load()

Set m_TreeView= tree_Activities.Object

End Sub

Private Sub Form_Close()

Set m_TreeView = Nothing

End Sub

Now you can bind your tree views events strongly typed via the
ComboBoxes on top of the VBA code window.
Set nd = tvw.Nodes(Me.txt_Activity_ID) '<----
Have you tried

MsgBox txt_Activity_ID.Value
Set nd = tvw.Nodes(txt_Activity_ID.Value)

and what does it contain? I assume that it is the plain ID as number
without a leading character.
What is the best way to determine that Item 1 is the second node belonging to
FY11? I know I could use nd.Next.key and if that generates an error, try nd.
Previous.key, but I was hoping there was a more elegant way.
I'm not sure, but take a look at Node.Index and/or
Node.FirstSibling.Index (Node.LastSibling.Index).


mfG
--> stefan <--
 
T

Tony Toews [MVP]

Dale_Fye via AccessMonster.com said:
I'm trying to remove a node from a treeview, elegantly.

I'm currently deleting the associated record from the underlying table, then
clearing the tree and rebuilding it (not very elegant).

From some code I recreated 12 years ago.

ActiveXCtl2.Nodes.Remove CStr(Me!vKey)

Where ActiveXCtl2 is the object name of the treeview control on the
form and me!vKey is the treeview node key..

TOny
 
D

Dale_Fye via AccessMonster.com

Thanks, Tony.

I finally figured it out myself this weekend.

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top