Really depends on what you mean by MaxLevel.
Longest branch in tree?
Longest branch that tree could have if all tables populated?
Last node in branch?
By MaxLevel I assume you mean the number of nodes in the longest branch of the tree. So if you click on Test2 it is has no children and it is the max node in that branch, but the tree has some branches with 4 levels.
In this case where you are unioning related tables the number of levels possible is the number of tables. You know how many tables and which table is the last. So it seems you could just provide a literal in your code. If you add more tables to your tree than modify your code. This is more reliable. Assume we know that the last possible nodes are Ribbon Controls. Their identifier is RibbonControl (explain later).
I would do
If TVW.SelectedNodeIdentifier = "RibbonControl" then ...
or
if TVW.getNodeIdentifier(TVW.SelectedNode) = "Ribbon Control" then ...
(There are Properties for the selected node and basically duplicate functions to get the same thing for any node that is why the two methods. In the Get functions you can pass any node not just the selected node.)
To explain how the queries work and help to tie the tree to records. Node queries get an "Identifier". This can be used to tell you which table a value comes from. (You can use the table name if you wanted but you will have to do a select case anyways so does not really help and may be harder to read.) The Identifier gets concatenated to the PK to and then used to form the node ID. The ParentID is the same thing with the parent Identifier concatenated to the parent FK.
When you load the tree the ID becomes the NodeKey which will be unique because event though all the PKs of the tables are autonumbers they now have the concatenated identifier. The Identifier gets stored in the Node tag property. Because of this, when you click on a node you can remove the Identifier from the Node key and get the PK. You can use the indentifier in a select case to then get the table.
qryNodeRibbonControl
qryNodeRibbonControl
ID | NodeText | ParentID | Identifier |
---|
RibbonControl1 | myButton1: button large | RibbonGroup2 | RibbonControl |
RibbonControl2 | mybutton2: button large | RibbonGroup2 | RibbonControl |
RibbonControl3 | myButton3: button large | RibbonGroup3 | RibbonControl |
RibbonControl4 | mybutton4: button large | RibbonGroup3 | RibbonControl |
RibbonControl5 | myButton5: button large | RibbonGroup4 | RibbonControl |
RibbonControl6 | mybutton6: button large | RibbonGroup4 | RibbonControl |
RibbonControl7 | myButton7: button large | RibbonGroup5 | RibbonControl |
RibbonControl8 | mybutton8: button large | RibbonGroup5 | RibbonControl |
RibbonControl9 | loginBtn: button large | RibbonGroup1 | RibbonControl |
However, normally this is done with a self referencing table. Think family tree. So the levels on any branch are undetermined and in theory infinite because you could keep adding children.
If there's no method for this, maybe I'll add a loop and extract the max level of nodes.
You cannot really do this with a loop (what would you loop?) You have to do this recursively just like loading the tree. And if I was going to do this that is where I would do it. I would make a property MaxPotentialLevels and set this when recursively loading the tree. If the value of MaxPotentialLevels is < node level then MaxPotentialLevels = Node Level
However that is problematic too. The biggest problem is most of the time I do not load the entire tree. I have the option to do a light load which means I load only the visible nodes. This saves tremendous amount of time on large trees. The nodes are only loaded when you expand a node. That means if you really wanted to do this you now need a seperate method to fully recurse the tree to figure out the longest branch.
This could be easily added, but not a lot of utility IMO. And has those potential problems. If it is a union query you will know the max level and better yet the last Identifier. For a self referencing table each node is the same (example person) so you do not care about longest branch in the tree. Unlikely you would do something different in that case.
Bottom line I think the most utility and most accurate would be to check the Identifier of the node to determine if it is the lowest level.
To determine if a node has children you can do something like
TVW.childNodes(TVW.SelectedNode).Count
If the count is 0 then it is at the end of a branch, but not necessarily the max possible level.