TreeView problem with Node.Parent

mrodent

New member
Local time
Today, 22:53
Joined
Jan 9, 2012
Messages
9
TreeView problem with Node.Parent [SOLVED]

Dear all,

There is a simple thing I want to do with a TreeView (NB Access 2000): for certain nodes I want them to expand... and expand their parent node, and grandparent node... etc. all the way up to root. Without this there is no guarantee that these nodes will be made viewable.

The code which works so far is to go:
Code:
If Not nodThis.Parent Is Nothing Then
            nodThis.Parent.Expanded = True
            If Not nodThis.Parent.Parent Is Nothing Then
                nodThis.Parent.Parent.Expanded = True
                If Not nodThis.Parent.Parent.Parent Is Nothing Then
                    nodThis.Parent.Parent.Parent.Expanded = True
                End If
            End If
End If
but if, as a first step to making this a loop or recursive method, I go
Code:
If Not nodThis.Parent Is Nothing Then
    parentnode = nodThis.Parent
        parentnode.Expanded = True
... this gives an error on line 2:
"Object variable or With variable not set"
Incidentally at the start of the function here, I have gone
"Dim parentnode As Node"
 
Last edited:
In VBA, the Set keyword is necessary to distinguish between assignment of an object and assignment of the default property of the object.
 
Thanks.

But I tried the following

Code:
        If Not nodThis.Parent Is Nothing Then
            Set parentnode = nodThis.Parent
            parentnode.Expanded = True
... and when I ran it I got "type mismatch" (on line 2)... any idea what I'm doing wrong?
 
Treeviews are not easy to debug without seeing the actual treeview in action.

What did you declare parentnode as?
 
Thanks... this is the code for the whole function... basically I want a node to be expanded all along its path if the "reminder date" is less than [today minus 2 days, the "prewarn" date]... otherwise the node should be collapsed.

I'm just finding it completely impossible to get hold of a Node object from nodThis.Parent. Utterly mystified.

As you see, parentnode is declared as "Node"...

Code:
Private Sub btnShowWarnings_Click()
    Dim db As DAO.Database, rst As DAO.Recordset, parentnode As Node
  
    Set db = CurrentDb
    Set rst = db.OpenRecordset("tblNodes", dbOpenDynaset, dbReadOnly)
    prewarnDate = DateAdd("d", 2, Date)
    For Each nodThis In Me.xTree.Nodes ' loop through all nodes
  
        nodeKeyLen = Len(nodThis.Key)
  
        intVal = Right(nodThis.Key, nodeKeyLen - 1)
        strCriteria = BuildCriteria("Index", rst.Fields("Index").Type, "=" & intVal)
        rst.FindFirst strCriteria
    
        remDate = rst("ReminderDate")
    
        If remDate <= prewarnDate Then
            nodThis.Expanded = True
            If Not nodThis.Parent Is Nothing Then
                nodThis.Parent.Expanded = True
                If Not nodThis.Parent.Parent Is Nothing Then
                    nodThis.Parent.Parent.Expanded = True
                    If Not nodThis.Parent.Parent.Parent Is Nothing Then
                        nodThis.Parent.Parent.Parent.Expanded = True
                        If Not nodThis.Parent.Parent.Parent.Parent Is Nothing Then
                            nodThis.Parent.Parent.Parent.Parent.Expanded = True
                        End If
                    End If
                End If
            End If
        Else
            nodThis.Expanded = False
        End If
    Next nodThis
    Me.xTree.SetFocus
End Sub
 
Can you provide me with a stripped down version of your db with just the relevant bits, i.e. table and form.
 
Hi,

Thanks for your interest...

I've done this... see attachment...

tblNodes is pretty self-explanatory... one thing to bear in mind is that a parent node must, as things stand, come before any offspring nodes in tblNodes if it is to be included in the tree
 

Attachments

  • Copy of kernel3.mdb
    Copy of kernel3.mdb
    560 KB · Views: 320
  • prewarn.gif
    prewarn.gif
    905 bytes · Views: 344
  • warn.gif
    warn.gif
    905 bytes · Views: 301
Last edited:
um... that previous post has an attached db (.mdb) and two gifs... which should go in the same directory...
 
@vbalnet...

just wondering if you had had a chance to look at this problem?
 
Hi mrodent,
While your waiting, have you thought about using a Nested Set Model (NSM) instead of the Adjacency Model for your table? with that it's much simpler to write queries to list all child nodes without recursion.
 
Hi ozinm,

I've just tried googling "nested set model" because I've never heard of this... is this used with a TreeView control? Or how visually is it represented? Do you have any examples of using this for a visual tree control? Thing is, treeview control is fairly simple... but this difficulty of grasping and using the .Parent property is frustrating... and no-one seems to know the answer...

The Wikipedia page on Nested Set Model warns that there might be quite a bit of "database thrash" with this...
 
Re: TreeView problem with Node.Parent [SOLVED]

cracked it. For reference:

Code:
    Dim currentNode As Variant

    Set currentNode = nodThis
    Do
        currentNode.Expanded = True
        If currentNode.Parent Is Nothing Then Exit Do
        Set currentNode = Me.xTree.Nodes(currentNode.Parent.Key)
    Loop

2 points
- Anything other than "Variant" gets "type mismatch" on Set currentNode = nodThis. The locals window reveals that the type of currentNode is down as "Variant/Object/INode". But declaring as "INode" gets "type mismatch".
- Use of "Set" is not optional.
 
A MSComCtlLib.Node object has an EnsureVisible() method. If you call that method on a particular node the TreeView will do whatever it needs to, including expanding parent nodes and scrolling, to do what the method name suggests. A code possibility...
Code:
  If remDate <= prewarnDate Then
    if nodThis.Children > 0 then
      [COLOR="Green"]'show the first child if there is one[/COLOR]
      nodThis.child[COLOR="DarkRed"].ensurevisible[/COLOR]
    else
[COLOR="Green"]      'otherwise at least show the current node[/COLOR]
      nodThis[COLOR="DarkRed"].ensurevisible[/COLOR]
    end if
  Else
    nodThis.Expanded = False
  End If
 
thanks for this...

I had indeed spotted "ensurevisible" and wondered about it... but it just seemed and still seems very peculiar that there is so much difficulty in obtaining the parent node (as a variable) from a given node... and presumably the same applies to everything else like "next sibling" and "previous sibling" etc.

Also, technically speaking, of course, ensurevisible also scrolls the node into view... which might not be wanted...
 

Users who are viewing this thread

Back
Top Bottom