Treeview nodes error (3 Viewers)

ClaraBarton

Registered User.
Local time
Today, 14:47
Joined
Oct 14, 2019
Messages
726
@MajP
When loading the treeview it completes but breaks at the last record

Code:
 Private Function FindRoot(ByVal nodX As Node, tvw As TreeView) As Node
   On Error GoTo ErrHandler
   Dim n        As Integer
  
    n = nodX.Index
Debug.Print nodX.Index
    While n <> nodX.Root.Index
        n = tvw.Nodes(n).Parent.Index
   Wend
  
ErrHandler:
       Set FindRoot = tvw.Nodes(n)
End Function

The error is error 91 object variable on parent.index
The same error is on closing at
Code:
For Each nd In tvw.TreeView.Nodes
in UpdateSortandLevel

I'm only using a small testing db but it always breaks at the last node.index (15)
is there a problem in my tables?
1758987902860.png

1758987972108.png
 
The root node won't have a .Parent property, so you get the Error 91.

You can handle it in your error handler and Resume Next for that err.Number.

But surely you know the root node using the expression to limit your While clause (nodX.Root.Index)

Why not bypass the whole While loop:
Code:
Private Function FindRoot(ByVal nodX As Node, tvw As TreeView) As Node
  Set FindRoot = tvw.Nodes(nodX.Root.Index)
End Function
?
 
Which treeview are you using?

Does Node.Root return the root node directly?

If so even simpler (almost tot the point of not needing a function at all!)
Code:
Private Function FindRoot(ByVal nodX As Node) As Node
  Set FindRoot = nodX.Root
End Function
 
Looking at that, I am not sure why I wrote it like that. I am thinking the Root property does not give you exactly what you expect. As @cheekybuddha points out you would think it would return the root node for that branch and no loop needed. I can only guess it does not.
I think in fact it only gives you the very first node and not the branch root node.
 
Last edited:
It is exactly as I expected. The root property is worthless. It returns node 0 every time.
I still think my code is a little weird and you should simply be checking if the nodes parent is nothing.
The root property returns the top level Node not the root node for the given branch. So it is pretty worthless.
See the results below. My code returns the Root node for a given branch. Purple.
The selected node is yellow.

Screenshot 2025-09-27 134859.png
 
This seems to be more correct.
Code:
Public Function FindRoot(ByVal nodX As Node, TVW As TreeView) As Node
   On Error GoTo ErrHandler
    
    While Not nodX.Parent Is Nothing
        Set nodX = nodX.Parent
    Wend
    Set FindRoot = nodX
    exit function
ErrHandler:
      MsgBox Err.Number & Err.Description & " in FindRoot"
End Function

I also can get rid of the second parmeter. Makes no sense and not needed, but then you need to change the calling functions too.
 
The original code is so bad and only works in spite of itself. Probably doing some drinking when I wrote that.
The root is the very first node so assume you are on a branch not on the first node branch. Such as Mozarell di.... And we know that the root property is Alfki and we really want ANATR.
So this line can never be true
Code:
While n <> nodX.Root.Index
And the code only works because of the error handler, which is not a good way to code.
It keeps checking the parent index until it gets to ANATR and then tries to get the parent property index which throws an error. The error returns the last viable node.
 
Last edited:
Well! there ya go! I worked and worked to keep from asking (always my fault!) and now you're taking the blame!!! I am so on this!
 
My getNodeLevel function was dumb too. Should be

Code:
Public Function GetNodeLevel(myNode As Node) As Integer
   'saving the PK as the key and the level in the value
   Dim intCount As Integer
   If Not myNode Is Nothing Then
      intCount = 1
      Do Until myNode.Parent Is Nothing
         Set myNode = myNode.Parent
         intCount = intCount + 1
      Loop
      GetNodeLevel = intCount
   End If
End Function

Before if first found the root node and traversed back until you found the root node. This meant it traversed the branch once to find the root node, then transverse it again just to count.
Does not sound like a big problem until you are doing this thousands of times on a large tree, and doubling the traversing.
 
Code:
Public Function getNodePK(theNode As Node) As String
    getNodePK = Replace(theNode.Key, theNode.Tag, "")
End Function
I always have trouble here too. Can never find theNode.Tag so calls it a type mismatch.
Is there a way just to return the ID?
Well the type mismatch ends up here: PK = CLng(TVW.getNodePK(nd))
 

Users who are viewing this thread

Back
Top Bottom