Use the strFunctionCode as the key value when adding nodes to the treeview. Then, attempt to add a new node to the treeview while trapping for errors that may occur. If an error is raised during the attempt to add a node, then you can be pretty sure one already exists with the same strFunctionCode:
On Error Resume Next
Set oNode = oTreeView.Nodes.Add('set whatever properties here)
If err.Number <> 0 Then
'An error was raised - node probably already exists
Else
'No error - node was created fine
End If
On Error Goto ErrHandler
Hope that helped. -Chris Didion
Matrix Automation, MCP
Loop through all the nodes..
You could use a function like this:
Private Function ItemExists(ByVal Text As String, ByVal CaseSensitive As Boolean) As Boolean
Dim loNode As Node
Dim lbRetval As Boolean
For Each loNode In lvwAddToGroup.Nodes
If CaseSensitive Then
If Text = loNode.Text Then
lbRetval = True
Exit For
End If
Else
If UCase$(Text) = UCase$(loNode.Text) Then
lbRetval = True
Exit For
End If
End If
Next loNode
ItemExists = lbRetval
End Function
And call it like this:
Dim lbExists As Boolean
lbExists = ItemExists("some node", False)
You can use the CaseSensitive parameter to determine whether "some node" is the same as "Some Node"..
I would tend to do what Chris Didion has suggested, you need to use the string you are adding as the key to the treeview. Create a function that tries to access the node. If the function fails then you know the node does not exist. Looping around each node every time you want to add one seems a bit overkill.
Here's the one I use
Public Function TreeViewNodeExists(tvwTreeView As TreeView, strKey As String) As Boolean
Dim strTmp As String
On Error GoTo TreeViewNodeExists_Err
TreeViewNodeExists = False
strTmp = tvwTreeView.Nodes(strKey).Text
TreeViewNodeExists = True
TreeViewNodeExists_Exit:
Exit Function
TreeViewNodeExists_Err:
Resume TreeViewNodeExists_Exit
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.