Finding text
To find if a string contains another string, you can use InStr
and InStrRev (InStrRev is only in VB6). These return the first and last
positions of a string contained in another string, respectively. They both use the
following syntax:
InStr$(Start,StringToSearch,StringToFind, SearchMode)
Start is an optional figure representing the position to start searching
the string. StringToSearch contains the string that you want to search. StringToFind
contains the string that you want to find. The following code gets the text from
txtFindString and highlights the first occurrence of that string in txtSearchString.
vbTextCompare tells the InStr function to find matching strings, regardless
of case. Use vbBinaryCompare for the search to be case sensitive.
Sub Command1_Click()
' Get the first occurrence of the string
StringPos = InStr(1, txtSearchString, txtFindString,
vbTextCompare)
If StringPos = 0 Then
' If StringPos returns 0, then the string is
not found
Msgbox "String not found"
Else
' Get the length of the string to find
StringLen = Len(txtFindString)
' Set the starting position of the selection
txtSearchString.SelStart = StringPos
' Set the selection length
txtSearchString.SelLength = StringLength
End If
End Sub
You could change InStr to InStrRev to find the last occurrence of the Find
String.
-
Posted by KöllMorgan on 14 Jan 2007
Here we go again:
After hours of debugging, I found out the only instance the first word on the second line does not get picked up is when I physically tab or hit enter and enter or type ad...
-
Posted by KöllMorgan on 14 Jan 2007
Replying to my own post...Hiya!
I found out the trick to grabbing the first word in the string using the above code:
InStr(1," " & YourString & " ", " " & YourSearc...
-
Posted by KöllMorgan on 13 Jan 2007
Hello!
Wonderful, helpful site, by the way...
I am working on a small program for a friend and noticed the below code. Works flawlessly, accept t...
Posted by koti_sastry on 06 Dec 2005
i know i am answering to an oldest question left unanswered..
but.. i just wanted to answer it..
just for some one who is searching on a specific topic ...and end up in finding only question...
-
Posted by fatwalrus on 17 Feb 2005
[b]I included a code extract here but you have to click on "read comments" to see it all.[/b]
[i]Basically it just searches the text string for 2 spaces in row, and replaces it with one space, the...