SEARCH in treeview

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

SEARCH in treeview

Post by sal21 »

How to search myvar in each level 3 of treeview?
Tks

User avatar
HansV
Administrator
Posts: 79287
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEARCH in treeview

Post by HansV »

Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

HansV wrote:
18 Jul 2024, 18:08
See [VBA] Treeview; search option
ok.

I USE THIS CODE TO SEARCH IN TREEVIEW.

if the value is found i set green font.

Only a problem...

if the treeview is compressed when the node is found i need to expand the root CN-CUNEO , to permit to the user to see the item found

How to?

Code: Select all


End Sub
Private Sub CERCA_COMUNI()

    Dim MYNODE As Node
    Dim NRTOT As Long
    
    VALORE = "004003"
    
    NRTOT = 0

    For Each MYNODE In Me.TreeView1.nodes
    
        If MYNODE.children = 0 Then
        
            NRTOT = NRTOT + 1
            
            ISTAT = Split(MYNODE, "-")(0)
            
            If ISTAT = VALORE Then
            Stop
            
            NR = MYNODE.Index
            
            MYNODE.BackColor = vbGreen
            
            Exit For
            
            End If
            
        End If
        DoEvents
    Next

End Sub

Last edited by sal21 on 19 Jul 2024, 10:14, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 79287
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEARCH in treeview

Post by HansV »

Have you tried

MYNODE.Selected = True
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

HansV wrote:
19 Jul 2024, 10:07
Have you tried

MYNODE.Selected = True
resolved also the last modified post!
Tks bro

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

HansV wrote:
19 Jul 2024, 10:07
Have you tried

MYNODE.Selected = True
is this a good tips:

...
VV = "063009-BOSCOTRECASE"
Set MYNODE = Me.TreeView1.nodes.Item(VV)
...

User avatar
HansV
Administrator
Posts: 79287
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEARCH in treeview

Post by HansV »

That will work if "063009-BOSCOTRECASE" is the Key of a node, not if it is the text of a node.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

HansV wrote:
19 Jul 2024, 18:24
That will work if "063009-BOSCOTRECASE" is the Key of a node, not if it is the text of a node.
Ahhhhh.
Tks

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

sal21 wrote:
19 Jul 2024, 18:54
HansV wrote:
19 Jul 2024, 18:24
That will work if "063009-BOSCOTRECASE" is the Key of a node, not if it is the text of a node.
Ahhhhh.

In this case i need only to loop, Is the unique way?

Tks

User avatar
HansV
Administrator
Posts: 79287
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEARCH in treeview

Post by HansV »

If your nodes do not have a Key, you do need to loop.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

:cheers:
HansV wrote:
19 Jul 2024, 19:07
If your nodes do not have a Key, you do need to loop.

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

HansV wrote:
19 Jul 2024, 19:07
If your nodes do not have a Key, you do need to loop.
During the fill of treeview:

...
NODX.Key="600223"

Correct?

User avatar
HansV
Administrator
Posts: 79287
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SEARCH in treeview

Post by HansV »

Yes. If you do that, you can later on use

VV = "600223"
Set MYNODE = Me.TreeView1.Nodes.Item(VV)

or even


VV = "600223"
Set MYNODE = Me.TreeView1.Nodes(VV)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4470
Joined: 26 Apr 2010, 17:36

Re: SEARCH in treeview

Post by sal21 »

:clapping:
HansV wrote:
19 Jul 2024, 21:32
Yes. If you do that, you can later on use

VV = "600223"
Set MYNODE = Me.TreeView1.Nodes.Item(VV)

or even


VV = "600223"
Set MYNODE = Me.TreeView1.Nodes(VV)

snb
5StarLounger
Posts: 610
Joined: 14 Nov 2012, 16:06

Re: SEARCH in treeview

Post by snb »

e.g.

Code: Select all

Private Sub UserForm_Click()
  With TV_01.Nodes("DOLO")
     .EnsureVisible
     .BackColor = vbGreen
  End With
End Sub