Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code which searches for one word in a text box, but how do I search for two words?

VB
If InStr(SourceC.Text, "word1") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If


I have tried the following, but that doesn't work.

VB
If InStr(SourceC.Text, "word1, word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If


Any help is much appreciated.
Posted
Updated 20-Aug-10 7:05am
v2

1 solution

This is the simple way:
VB
If InStr(SourceC.Text, "word1") Or InStr(SourceC.Text, "word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If

If you're using VB.Net, you can also make use of regular expressions:
VB
If System.Text.RegularExpressions.Regex.IsMatch(SourceC.Text, "word1|word2") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If

When you get a little more familiar with regular expressions, you can also make that a bit shorter:
VB
If System.Text.RegularExpressions.Regex.IsMatch(SourceC.Text, "word[12]") Then
	form2.Show()
	Me.Hide()
Else
	form1.Show()
	Me.Hide()
End If

Though I'm guessing your needs are not quite that specific. Also, some of that code might have to change depending on if you want to make sure BOTH the words appear at the same time, and if overlaps count (e.g., if the word "word12" will count towards both "word1" and "word12", because the former is a subset of the latter). Also, you could use "Contains" rather than "InStr".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900