Retrieving keys for Treeview

Can somebody provide me with the code to retrieve the parent key when clicking on a child in a Treeview.  

Thanks

DebeGProgrammerAsked:
Who is Participating?
 
fds_fatboyCommented:
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If Not Node.Parent Is Nothing Then
        If Not Node.Parent.Parent Is Nothing Then
            MsgBox Node.Parent.Parent.Key
        End If
    End If
End Sub
'How many levels do you want me to go to?
0
 
fds_fatboyCommented:
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    If Not Node.Parent Is Nothing Then
        MsgBox Node.Parent.Key
    End If
End Sub
0
 
DebeGProgrammerAuthor Commented:
Ok, that was easy enough and I will give you credit.  But, what if I wanted to retrieve the key of a parent two levels up, or the parent of a parent.  
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
fds_fatboyCommented:
'This is more generic, more flexible (and a lot more code)
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    MsgBox GetParentNodeKey(Node,2)
End Sub

Private Function GetParentNodeKey(Node As MSComctlLib.Node, ByVal Level As Long)  As String
'Level - 1 = Parent, 2 = Grandparent, 3 = Great-grandparent etc
    Dim ParentNode As MSComctlLib.Node
    Dim LevelIndex As Long

    Set ParentNode = GetParentNode(Node, Level)
    If Parent Node Is Nothing Then
        GetParentNodeKey = vbNullString
    Else
        GetParentNodeKey = ParentNode.Key
    End If

    Set ParentNode = Nothing
End Function

Private Function GetParentNode(Node As MSComctlLib.Node, ByVal Level As Long) As MSComctlLib.Node
'Level - 1 = Parent, 2 = Grandparent, 3 = Great-grandparent etc
    Dim ParentNode As MSComctlLib.Node
    Dim LevelIndex As Long

    Set ParentNode = Node

    For LevelIndex = 1 to Level
         If ParentNode Is Nothing Then Exit For
         Set ParentNode  = ParentNode.Parent
    Next

    Set GetParentNode = ParentNode
    Set ParentNode = Nothing
End Function
0
 
DebeGProgrammerAuthor Commented:
For some reason when I type in NODE. a list doesn't appear and this is my first time working with treeviews so I didn't know what my options were.   Thanks for your assistance.
0
 
fds_fatboyCommented:
>>  ...a list doesn't appear...

This normally happens when there is something in the code causing intellisense to get confused. Check your references and declarations (Variables/Subs/Functions/properties etc) - these are the most likely causes.

If you hit ctrl+F5 to run with full compile, this will point out any problems with the code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.