Solved

Read Nodes in Treeview

Posted on 2010-08-26
12
488 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:Sasha42
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33533261
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.
0
 
LVL 75
ID: 33533738
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
0
 
LVL 75
ID: 33533743
More:

Here are step by step Treeview instruction with source code and references
           http://www.databasejournal.com/features/msaccess/article.php/3734331/Access-TreeView-ListView-Basics.htm

http://support.microsoft.com/kb/209891/en-us
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 33534758
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



0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 33534778

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
0
 

Author Comment

by:Sasha42
ID: 33535038
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
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33535264
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.
0
 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33535295
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;
}
0
 

Author Comment

by:Sasha42
ID: 33535322
Thanks!! Sorry, I don't understand the code you wrote.  What is { ?  
0
 
LVL 2

Expert Comment

by:SkydiverFL
ID: 33535573
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
0
 

Author Comment

by:Sasha42
ID: 33535966
Thanks again.  I cannot get it to work in VBA.  I have tried several variations.  
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 33537725
Instead of using the doubleclick event, use the NodeClick event.  Try:

Private Sub axTreeView_NodeClick(ByVal Node as Object)

    Dim nd as MSComctlLib.Node

    set nd = Node
    while not nd.Parent is nothing
        set nd = nd.Parent
    Wend

    debug.print nd.key, nd.text
    docmd.openform "YourFormName",, , "[AllegationNumber] = '" & nd.key & "'"

End Sub

This assumes that AllegationNumber is actually a string, since key values must be strings.






0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now