How to search a textbox for listbox items.(Solve)
Hi, I know how to search a listbox for an item on a texbox, but now I need to search a textbox for any for items locoted in a listbox.
so lets say if a listbox has, one, two, three
and in the textbox there is a sentence as such
"the number one"
then the program should msgbox word found.
thanks :)
Re: How to search a textbox for listbox items.
You should be using Instr$() function. Something like this
VB Code:
If InStr$(Text1.Text, List1.List(1)) > 0 Then
MsgBox "List Item is present in the text box"
End if
Re: How to search a textbox for listbox items.
Not all that many ways to go:
VB Code:
Dim strText As String, lngA As Long
' store into temporary string variable for faster access
strText = Text1.Text
' loop!
For lngA = 0 To List1.ListCount - 1
If InStr(strText, List1.List(lngA), vbTextCompare) > 0 Then
MsgBox "Found!"
Exit For
End If
Next lngA
If lngA = List1.ListCount Then MsgBox "Not found!"
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
Re: How to search a textbox for listbox items.
well here's one way it could be done:
VB Code:
Dim N As Long
For N = 0 To List1.ListCount
If InStr(Text1.Text, List1.List(N)) Then _
MsgBox "Found: " & List1.List(N)
Next N
but what approach you actually take depends entirely on the specific circumstances of what you want to do. For example: do you want whole / partial words? is there punctuation?
you might want to also consider searching for Regular Expressions.
Re: How to search a textbox for listbox items.
Okie I try this one first :)
VB Code:
Dim strText As String, lngA As Long
' store into temporary string variable for faster access
strText = Text6.Text
' loop!
For lngA = 0 To List5.ListCount - 1
If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then
MsgBox "Found!"
Exit For
End If
Next lngA
If lngA = List5.ListCount Then MsgBox "Not found!"
but i get a type mistmatch error on this line
VB Code:
If InStr(strText, List5.List(lngA), vbTextCompare) > 0 Then
let me check the others and see
Thanks guys
Re: How to search a textbox for listbox items.
Quote:
Originally Posted by Merri
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
the VB IDE won't even let you do it - it automatically removes the $ for you.
Re: How to search a textbox for listbox items.
Aigh I try using
VB Code:
Dim N As Long
For N = 0 To List5.ListCount
If InStr(Text6.Text, List5.List(N)) Then _
MsgBox "Found: " & List5.List(N)
Next N
it almost works perfect, but it somehow also looks for empty spaces, I think cause it also sends a message of Found: like that without any words
:rolleyes:
ah I looking to match either words or phrases :)
Re: How to search a textbox for listbox items.
You must do ListCount - 1 and also check if the value is above 0, because -1 is reserved for error messages.
Re: How to search a textbox for listbox items.
in post #7 (and in my orig post #4) it should be List5.ListCount - 1.
In fact, you should have got an error - oh well
I think you'd actually be much better off using Regular Expressions.
Re: How to search a textbox for listbox items.
Quote:
Originally Posted by Merri
Shuja Ali: you shouldn't use $ character within InStr, because the return value is a Long and if you use $, the return value is first converted to a string and then to something else for numeric comparison.
I usually write $ for almost all the string functions (this is a habit I have rright from Qbasic days), and this is what you get when you write code without VB IDE. :confusion:
Re: How to search a textbox for listbox items.
Thanks guys now it works great i ended up using
VB Code:
Dim N As Long
For N = 0 To List5.ListCount - 1
If InStr(Text6.Text, List5.List(N)) Then _
MsgBox "Found: " & List5.List(N)
Next N
Thanks :)