Hello,
I'm trying to build a function that splits the nodes of a treeview in order to make it searchable by using the boolean terms AND, OR, and NOT.
The search is intended to be done by using just one textbox
So, here is what I have until now:
1. The event which loads the treeview and perform the search.
2. The code of the Function
3. The results in the immediate window
As you may see, my big problem is building the array. varWords returns an Error 9 "Subscript out of range". I tried several ways to solve this error, unsuccesfuly.
I really appreciate any help or guidance.
Regards,
Diego
I'm trying to build a function that splits the nodes of a treeview in order to make it searchable by using the boolean terms AND, OR, and NOT.
The search is intended to be done by using just one textbox
So, here is what I have until now:
1. The event which loads the treeview and perform the search.
Code:
[COLOR=#0066ff]Dim[/COLOR] booCurrMatch [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Boolean[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] booMatched [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Boolean[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] lngLoop [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Long[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] strOperator [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] strWord [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] varWords [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Variant[/COLOR]
varWords = GetWords(Me.Text2.Text)
[COLOR=#0066ff]If[/COLOR] [COLOR=#0000ff]Len[/COLOR](Me.Text2.Text & [COLOR=#888800]""[/COLOR]) > 0 [COLOR=#0000ff]Then[/COLOR]
[COLOR=#0000ff]For[/COLOR] [COLOR=#0066ff]Each[/COLOR] nodNode In tv.Nodes
booMatched = (InStr(1, nodNode.Text, varWords(1), vbTextCompare) > 0)
[COLOR=#0066ff]If[/COLOR] UBound(varWords) > 0 [COLOR=#0000ff]Then[/COLOR]
[COLOR=#0000ff]For[/COLOR] lngLoop = 1 [COLOR=#0000ff]To[/COLOR] UBound(varWords)
strOperator = Left(varWords(lngLoop), InStr(varWords(lngLoop), [COLOR=#888800]" "[/COLOR]) - 1)
strWord = [COLOR=#0000ff]Mid[/COLOR](varWords(lngLoop), InStr(varWords(lngLoop), [COLOR=#888800]" "[/COLOR]) + 1)
booCurrMatch = (InStr(1, nodNode.Text, strWord, vbTextCompare) > 0)
[COLOR=#0066ff]Select[/COLOR] [COLOR=#0066ff]Case[/COLOR] strOperator
[COLOR=#0066ff]Case[/COLOR] [COLOR=#888800]"And"[/COLOR]
booMatched = booMatched [COLOR=#bb00ff]And[/COLOR] booCurrMatch
[COLOR=#0066ff]Case[/COLOR] [COLOR=#888800]"Or"[/COLOR]
booMatched = booMatched [COLOR=#bb00ff]Or[/COLOR] booCurrMatch
[COLOR=#0066ff]Case[/COLOR] [COLOR=#888800]"Not"[/COLOR]
booMatched = booMatched [COLOR=#bb00ff]And[/COLOR] ([COLOR=#bb00ff]Not[/COLOR] booCurrMatch)
[COLOR=#0066ff]End[/COLOR] [COLOR=#0066ff]Select[/COLOR]
[COLOR=#0000ff]Next[/COLOR] lngLoop
[COLOR=#0066ff]End[/COLOR] [COLOR=#0066ff]If
[/COLOR]
2. The code of the Function
Code:
[COLOR=#0066ff]Function[/COLOR] GetWords(Expression [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]) [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Variant[/COLOR]()
[COLOR=#0066ff]Dim[/COLOR] vItem() [COLOR=#0000ff]As[/COLOR] [COLOR=#0000ff]String[/COLOR]
[COLOR=#0066ff]Dim[/COLOR] l [COLOR=#0000ff]As[/COLOR] [COLOR=#6600ff]Long[/COLOR]
vItem = Split(Expression, [COLOR=#888800]" "[/COLOR])
[COLOR=#0000ff]For[/COLOR] l = LBound(vItem) [COLOR=#0000ff]To[/COLOR] UBound(vItem)
Debug.Print [COLOR=#888800]"vItem 0="[/COLOR] & vItem(0), l
Debug.Print [COLOR=#888800]"vItem 1="[/COLOR] & vItem(1), l
Debug.Print [COLOR=#888800]"vItem 2="[/COLOR] & vItem(2), l
[COLOR=#0000ff]Next[/COLOR]
[COLOR=#0066ff]End[/COLOR] [COLOR=#0066ff]Function
[/COLOR]
3. The results in the immediate window
Code:
vItem 0=Sistema 0
vItem 1=AND 0
vItem 2=Nervioso 0
vItem 0=Sistema 1
vItem 1=AND 1
vItem 2=Nervioso 1
vItem 0=Sistema 2
vItem 1=AND 2
vItem 2=Nervioso 2
I really appreciate any help or guidance.
Regards,
Diego
Last edited: