Solved

Read Nodes in Treeview

Posted on 2010-08-26
12
508 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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
 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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 …

785 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