Re: Treeview help




<Fern G> wrote in message news:ubx6w6WCGHA.916@xxxxxxxxxxxxxxxxxxxxxxx
> Hi Mike,
>
> I use this
>
> Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))
>
> to add my nodes. Do you know how I can obtain 'mynod' when there
> is a duplicate key, since the above will be an error, hence I need to
> set the node, without adding it.

Well, since you're getting a duplicate key error, you know that a node
having that key must already exist. So, just use that key to get a reference
to the node from the Nodes collection. Here's an example (air code):


On Error GoTo EH

Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))
mynod.Bold = Not mynod.Bold

Exit Sub

EH:

If Err.Number = 35602 Then 'duplicate key error number
Set mynod = Treeview1.Nodes(mynod.Key)
Resume Next
Else
MsgBox Err.Description
End If

End Sub

Note that mynod must already be assigned a reference to a Node object
(otherwise, you'll get an 'object variable or with block variable not set'
error). I was just building on the line of code you posted.

Here's tested code that's a better example:

Private Sub Form_Load()

Dim mynod As Node

On Error GoTo EH

Set mynod = TreeView1.Nodes.Add(, , "Test", "Test")

'Try to add a duplicate
Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, "Test", "Test")

mynod.Bold = Not mynod.Bold

Exit Sub

EH:

If Err.Number = 35602 Then 'duplicate key error
Set mynod = TreeView1.Nodes(mynod.Key)
Resume Next
Else
MsgBox Err.Description
End If

End Sub


--
Mike
Microsoft MVP Visual Basic


.