Link to home
Start Free TrialLog in
Avatar of Sharon
SharonFlag for United States of America

asked on

Read Nodes in Treeview

I have three levels of nodes.  I can get the value of the current node.  How do I get the value of the node above it?  Thanks.
Avatar of SkydiverFL
SkydiverFL
Flag of United States of America image

If this is .NET, you should be able to get node.Parent to find the parent node.  As for the "value" it depends on what you want.  If you have stored something in the Tag, just look at node.Parent.Tag or node.Tag.  If you are looking for the text (most common), just grab node.Text or node.Parent.Text.  Remember, a TreeNode is a DISPLAY element so they do not contain a value per se.
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Mike ... check this out:

  http://datapigtechnologies.com/AccessMain.htm

(Scroll down to bottom until you get to Access Tools)

Full mdb examples and online video tutorial ... it's a great jump start.

mx
Technically, nodes don't have values.  Are you trying to determine the key or the text associated with a nodes parent?  What event are you using?  If you are using an event that already displays the node object (like Collapse, Expand, NodeClick, ...) then you can use something like:

    node.parent.key




However, that will raise an error if the node is in your upper most level.  I usually set the Tag property of the node to the level as I create them, this way I can use that property to determine what level of the tree the node is at.

    if node.tag <> 1 Then
        debug.print node.parent.key
    end if
Avatar of Sharon

ASKER

Below is the code that builds the tree.  I have code that when I double click on John Doe it returns the text - Set CurNode = Me!axTreeView.SelectedItem.  What i need is the CaseNumber (parent) so that I can open a form with the CaseNumer and the Person's Name.  Thanks.

The tree looks this:
CaseNumber123
CaseNumber456
  +Witness
        John Doe
+Victim
        Moe Stooge

sSQL = "select * from tblcases"
     
    Set rs = db.OpenRecordset(sSQL, dbOpenForwardOnly)
   
     Do Until rs.EOF
        Me!axTreeView.Nodes.Add , , rs!AllegationNumber, rs!AllegationNumber
        rs.MoveNext
    Loop
    rs.Close
   
     'Fill Level 2.
    Set rs = db.OpenRecordset("qryAssociationCategory1", dbOpenForwardOnly)
    Do Until rs.EOF
        strOrderKey = StrConv("o" & rs!AssociationCatagory, vbLowerCase)
        Me!axTreeView.Nodes.Add rs!AllegationNumber.Value, tvwChild, strOrderKey, _
           rs!AssociationCatagory
        rs.MoveNext
    Loop
    rs.Close
   
    ' Fill Level 3.
 Set rs = db.OpenRecordset("qryAssociationCategory2", dbOpenForwardOnly)
    Set rs = db.OpenRecordset("tblActivities", dbOpenForwardOnly)
    Do Until rs.EOF
        strOrderKey = StrConv("o" & rs!AssociationCatagory, vbLowerCase)
        strProductKey = StrConv(strOrderKey & "p" & rs!PersonID, vbLowerCase)
        Me!axTreeView.Nodes.Add strOrderKey, tvwChild, strProductKey, _
           rs!PersonProviderName
        rs.MoveNext
    Loop
    rs.Close
The challenge that you are going to have is knowing exactly WHAT you are selecting.  Based on your explanation, and the code provided, the user can click on ANY node... Case, Witness, Person, etc.  You're gonna have to walk the tree back upward to find the case.

For example:

TreeNode curNode = selectedNode;
while ( curNode.Parent != null )
{
     curNode = curNode.Parent;
}

This will give you the top-most parent... the case.  Then, just evaluate the final curNode for the value you want.
Expanded example...

public TreeNode getRoot ( TreeNode node )
{
     if ( node == null ){ return null; } // Nothing was supplied ... gracefully exit.
     if ( node.Parent == null ){ return node; }  // Supplied node is the root.

     TreeNode curNode = selectedNode;
     while ( curNode.Parent != null )
     {
          curNode = curNode.Parent;
     }
     
     return curNode;
}
Avatar of Sharon

ASKER

Thanks!! Sorry, I don't understand the code you wrote.  What is { ?  
Sorry... it was C#.  The equivalent VB should be...

    Function getNode(ByVal node As TreeNode) As TreeNode
        If node Is Nothing Then Return Nothing
        If node.Parent Is Nothing Then Return Nothing

        Dim curNode As TreeNode
        curNode = Nothing
        Do While curNode.Parent IsNot Nothing
            curNode = curNode.Parent
        Loop

        Return curNode
    End Function
Avatar of Sharon

ASKER

Thanks again.  I cannot get it to work in VBA.  I have tried several variations.  
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial