How to: Search and filter nodes
Find nodes by their text, select them one after another, and limit the tree to the nodes that match a condition.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
SearchText highlights every node whose text contains the search text, and FindNext selects the
matches one by one; users can do the same in the search bar (Ctrl+F, AllowSearch). Filter
hides every node except those that match a WHERE condition or a JPath expression, along with their
ancestors. SelectNode and NodeExists find a single node by its path.
Implementation
With Me!Tree1.Object
.SearchText = "Ca" ' highlights matches, loaded or not
Debug.Print .SearchCount & " matches"
.FindNext ' selects the next match; .FindNext True: previous
.SearchText = "" ' ends the search
.Filter = "Active = True" ' WHERE condition of the record source, like Form.Filter
.Filter = "$..[?(@.Checked == true)]" ' or JPath over the loaded nodes
.Filter = "" ' shows everything, view as before the filter
If .NodeExists("United States\Texas") Then .SelectNode "United States\Texas"
Debug.Print UBound(.FindNodes("$..[?(@.Level == 2)]")) + 1 & " nodes on level 2"
End With
Remarks
- The search counts up to 1,000 matches. It queries the record source (
LIKE '*text*'onTextField) unless the whole tree is loaded (LazyLoad = FalseorGroupFields), in which case it scans the loaded nodes. - A WHERE filter queries the record source every time you set it; with a pass-through query, the whole query runs on the server.
- Text path comparisons are case-insensitive and use
PathSeparator(backslash by default). WithLazyLoad, only the levels along the path are loaded. - JPath runs over a JSON view in which each node has
Key,Text,ParentKey,Image,SortValue,Checked,Level, andNodes;FindNodesloads the whole tree first. - In the search bar, F3 and Shift+F3 move between matches, Enter selects the next one, and Matches only filters the tree to the matches.
Related topics
en/wiki/msaccess/comctl/how-to/search-and-filter.txt · Last modified: by Wolfgang van der Stille