Solved

Word Searching in a text box

Posted on 1999-01-13
8
183 Views
Last Modified: 2010-05-03
How can I make a procedure that will search through a block of text in a text box (or other control) and find a specified word or words within it.
0
Comment
Question by:Help_me
8 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1455971
Use the Instr command
0
 
LVL 18

Expert Comment

by:deighton
ID: 1455972
Here is an example

Private Sub Command1_Click()

    Dim c As Long
   
    c = InStr(Text1.Text, "hello")
   
    If c > 0 Then
   
        MsgBox "hello" & " found at position " & CStr(c)
       
    End If


End Sub


0
 

Author Comment

by:Help_me
ID: 1455973
I've boosted the points because this one's important. Following on from the above procedure, I need to expand this utility so that it:

     searches through the text and finds the word (as above), and highlights it. If the word is out of view, the program should scroll down to it.

     I would also like to be able to do a word count.


0
Technology Partners: 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 18

Expert Comment

by:deighton
ID: 1455974
Looks like you'll need to use the rich text box.  I can work on it
0
 
LVL 1

Expert Comment

by:ikxsoft
ID: 1455975
to highlight the word use the sellength, selstart properties of the text box

selstart specifies where to start highlighting, the instr function will give you this
sellength is how many characters you want to highlight
0
 

Author Comment

by:Help_me
ID: 1455976
I can do this using the rich text box, although it will not actually select the text, it still appears unselected despite the SelStart and SelLength properties containing the correct values. I also need to know how to load text into the box using the common dialog box.
0
 

Author Comment

by:Help_me
ID: 1455977
Que?
0
 
LVL 1

Accepted Solution

by:
bear454 earned 150 total points
ID: 1455978
Assuming a richtextbox named RichTextBox1, and a common dialog name CommonDialog1, and a command button named Command1, the file could be loaded as follows:

Private Sub Command1_Click()
    CommonDialog1.ShowOpen
    On Error GoTo ErrHandle
    RichTextBox1.LoadFile CommonDialog1.filename
    Exit Sub
ErrHandle:
End Sub

Given a textbox containing the desired search string named Text1, and a command button for searching named Command2, the following will select and highlight the substring, progressing through the document.
(Assumes RichTextBox1.HideSelection = False)

Private Sub Command2_Click()
    If RichTextBox1.Find(Text1.Text, _
      IIf(RichTextBox1.SelLength = 0, 0, _
      RichTextBox1.SelStart + RichTextBox1.SelLength), _
      Len(RichTextBox1.Text) - 1) = -1 Then
        RichTextBox1.SelStart = 0
        RichTextBox1.SelLength = 0
        MsgBox "Finished searching document."
    End If
End Sub

Any other questions? =}

0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

735 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