Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Searching the whole word in the text box

Posted on 2002-07-27
8
Medium Priority
?
354 Views
Last Modified: 2010-05-02
Maybe anybody have a procedure ,how to search a whole word in the text box .

The same functionality like Ctrl F (Whole word only)

Best regards,
Irina
0
Comment
Question by:irina_1973
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:adam8
ID: 7182816
Hi,
I developed a search function for you. I have checked it but there still may be bugs.

'HERE IT IS
Option Explicit
Dim ePos As Long, sPos As Long

Function FindWord(SearchWord As String, TextBoxA As TextBox, Optional StartPosition As Long = 0, Optional CaseSensitive As Boolean = False) As Boolean
    Const Chars = "0123456789abcdefghijklmnopqrstuvwxyz"
    Dim i As Long
    Dim a As Integer, b As Integer
    Dim Search As String, TBox As String
   
    If Trim$(SearchWord) <> SearchWord Then
        MsgBox "You can not search for whole words while you search time has more than one word."
        FindWord = False
        GoTo NoMatch
    End If
   
    Search = SearchWord
    TBox = TextBoxA
   
    FindWord = False
    ePos = 0
    sPos = StartPosition
    i = 0
    If CaseSensitive = False Then Search = LCase$(Search): TBox = LCase$(TBox)
   
    Do
        i = i + 1
        i = InStr(i, TBox, Search)
        If i = 0 Then GoTo NoMatch
       
        If i > 0 Then
            sPos = i
            ePos = i + Len(Search)
           
            If sPos - 1 > 0 Then
                a = InStr(1, Chars, Mid$(LCase$(TBox), sPos - 1, 1))
            Else
                a = 0
            End If
           
            If ePos > Len(TBox) Then
                b = 0
            Else
                b = InStr(1, Chars, Mid$(LCase$(TBox), ePos, 1))
            End If
           
            If a = 0 And b = 0 Then
                i = 0
                FindWord = True
            End If
        End If
       
    Loop Until i = 0
   
Exit Function
NoMatch:
sPos = 0
ePos = 0
FindWord = False
End Function


<<<<<<<<<<<THIS IS AN EXAMPLE OF HOW TO USE IT>>>>>>>>

Private Sub Command1_Click()
    Dim ret As Boolean
    ret = FindWord("", Text1)
   
    If ret = True Then
        'Select the text that the user searched for.
        Text1.SelStart = sPos - 1
        Text1.SelLength = ePos - sPos
        Text1.SetFocus
       
    Else
        MsgBox "No match string was found"
    End If
End Sub

If you need me to send you the sample project I made please let me know. If there are any bugs please let me know.
0
 
LVL 1

Expert Comment

by:adam8
ID: 7182826
By the way I will quickly explain how the use the function..

Function FindWord(SearchWord As String, TextBoxA As TextBox, Optional StartPosition As Long = 0, Optional CaseSensitive As Boolean = False) As Boolean

The search Word is what word you want to look for.

the TextBoxA is the text box you want to search in.

the Startposition is the spot in the text box you want to start searching from (21 would mean 21 characters from the start)

CaseSensitive explains itself.


There is one error I found in the code. I forgot to fix it before i submitted it.

In the Main search function where you have the following code.

   sPos = StartPosition
   i = 0

   CHANGE IT TO .....

    sPos = 0
    i = StartPosition - 1
0
 
LVL 2

Expert Comment

by:yongyih
ID: 7182902
try this:

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP"   ' String to search in.
SearchChar = "P"   ' Search for "P".

' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)  

' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)

' Comparison is binary by default (last argument is omitted).
MyPos = Instr(SearchString, SearchChar)   ' Returns 9.

MyPos = Instr(1, SearchString, "W")   ' Returns 0.

hope this can help you. good luck. ^_^
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:jayeshshah
ID: 7183164
more better you can use the instring function as below

if instr(1,"String1",String2 & " ")>0 then
  Msgbox "Word found"
else
  Msgbox "Word could not be located"
end if
0
 
LVL 1

Accepted Solution

by:
adam8 earned 200 total points
ID: 7183228
My search routine is similar to doing a "Find whole words only" search in Microsoft Office. It will only return a match if the exact search term is found in the textbox. If there is a full stop or comma after the word my search function will take that into account as well, unlike the other examples posted here so far.

example:

Text1 = "hello, how are you?"

If you did a search for "Hello" with my search function it would take the comma after the hello into account. The comma doesn't alter the word hello.
0
 
LVL 1

Expert Comment

by:adam8
ID: 7183236
I noticed a few errors in my previous search function.

Instead of using the first one I posted use this one.

Option Explicit
Dim ePos As Long, sPos As Long

Function FindWord(SearchWord As String, TextBoxA As TextBox, Optional StartPosition As Long = 1, Optional CaseSensitive As Boolean = False) As Boolean
    Const Chars = "0123456789abcdefghijklmnopqrstuvwxyz"
    Dim i As Long
    Dim a As Integer, b As Integer
    Dim Search As String, TBox As String
   
    If Trim$(SearchWord) <> SearchWord Then
        MsgBox "You can not search for whole words while you search time has more than one word."
        FindWord = False
        GoTo NoMatch
    End If
   
    Search = SearchWord
    TBox = TextBoxA
   
    FindWord = False
    ePos = 0
    sPos = 0
    i = StartPosition - 1
   
    If CaseSensitive = False Then Search = LCase$(Search): TBox = LCase$(TBox)
   
    Do
        i = i + 1
        i = InStr(i, TBox, Search)
        If i = 0 Then GoTo NoMatch
       
        If i > 0 Then
            sPos = i
            ePos = i + Len(Search)
           
            If sPos - 1 > 0 Then
                a = InStr(1, Chars, Mid$(LCase$(TBox), sPos - 1, 1))
            Else
                a = 0
            End If
           
            If ePos > Len(TBox) Then
                b = 0
            Else
                b = InStr(1, Chars, Mid$(LCase$(TBox), ePos, 1))
            End If
           
            If a = 0 And b = 0 Then
                i = 0
                FindWord = True
            End If
        End If
       
    Loop Until i = 0
   
Exit Function
NoMatch:
sPos = 0
ePos = 0
FindWord = False
End Function
0
 

Author Comment

by:irina_1973
ID: 7183243
Thank you ,adam8!
You helped me a lot .
Thanks you that you free you time to write it .

Also ,thanks to yongyih, jayeshshah.
It's also very useful information for me .
0
 
LVL 1

Expert Comment

by:adam8
ID: 7183259
Thank you for accepting my answer,

remember to use the newest function that I posted which is the one I fixed up a few problems with.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question