Results 1 to 10 of 10

Thread: Search And Find

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Search And Find

    Hello, fellow coders,
    I've gotten some powerful code from here, yet I've got Soooo much to learn. Can anyone tell me how to make the following code stop at each find, instead of shooting through the whole search without stopping? This code simply totals up the amount of times the search text was found and turns them all red. I expect it to be simple, but simple is hard when you don't know what it is. Thanks in advance.

    Option Explicit
    Option Compare Text

    Private Sub Command2_Click()
    unload me
    End Sub

    Private Sub Form_Load()

    RichTextBox1.LoadFile "MyFile.wav"

    End Sub

    Private Sub Command1_Click()

    Dim strval As String 'Inputbox returns a string
    Dim nStrings As Long
    RichTextBox1.LoadFile "MyFile.wav"
    strval = " " & InputBox("Enter the string to find.", "Findit", _
    "Love") & " "

    If strval <> "" Then

    nStrings = FindIt(RichTextBox1, strval)
    MsgBox (Str$(nStrings + 1) & " instances of """ & strval & """ found.")

    End If

    End Sub

    Private Function FindIt(Box As RichTextBox, Srch As String, _
    Optional Start As Long)

    Dim retval As Long 'Instr returns a long
    Dim Source As String 'variable used in Instr

    Source = Box.Text 'put the text to search into the variable

    If Start = 0 Then Start = 1 'the initial call doesn't pass a value
    'for Start, so it will equal 0

    retval = InStr(Start, Source, Srch)

    If retval <> 0 Then

    With Box
    .SelStart = retval - 1
    .SelLength = Len(Srch)
    .SelColor = vbRed
    .SelBold = True
    .SelLength = 0
    End With

    Start = retval + Len(Srch)

    FindIt = 1 + FindIt(Box, Srch, Start)

    End If
    End Function
    Last edited by GARY MICHAEL; Dec 18th, 2002 at 02:40 AM.
    Thanks,
    GARY

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    This isn't an answer to your question but I just wanted to point out that this code will cause you problems.
    VB Code:
    1. Private Sub Command2_Click()
    2. End
    3. End Sub
    Instead of using End you should just unload all your forms.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378
    Thanks for your advice, Martinliss. I've changed the code from "End" to "Unload Me".
    Thanks,
    GARY

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    It is at this point:

    VB Code:
    1. If retval <> 0 Then
    2.     With Box
    3.                 'etc
    4.     End With
    5.     Start = retval + Len(Srch)
    6. 'Now we re-call the function to find the next occurrence of the string
    7.     FindIt = 1 + FindIt(Box, Srch, Start)
    8. End If

    So, just befre you call "FindIt" again, you can do things to pause the program (Msgbox "Click to Continue")

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378
    Thanks, JordanChris

    Your code worked well. How can I replace the msgbox with a next button so that it would work like a regular search routine and stop at each highlighted find? Thanks in advanced to anybody who would respond.
    Thanks,
    GARY

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Instead of using the Find method of the richtextbox, i use while loops to search the text and colorize it/count it. Tis how i made my error report for my programming language. Here's how i'd do that:

    VB Code:
    1. Option Explicit
    2. Dim Position As Integer
    3. Dim found As Integer
    4.  
    5. Private Sub Command2_Click()
    6. unload me
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10.  
    11. RichTextBox1.LoadFile "MyFile.wav"
    12.  
    13. Position = 0
    14.  
    15. found = 0
    16.  
    17. End Sub
    18.  
    19. Private Sub Command1_Click()
    20.  
    21. Dim strval As String 'Inputbox returns a string
    22. Dim nStrings As Long
    23. RichTextBox1.LoadFile "MyFile.wav"
    24. strval = " " & InputBox("Enter the string to find.", "Findit", _
    25. "Love") & " "
    26.  
    27. If strval <> "" Then
    28.  
    29. FindIt RichTextBox1, strval
    30. MsgBox (found & " instances of """ & strval & """ found.")
    31.  
    32. End If
    33.  
    34. End Sub
    35.  
    36. Private Function FindIt(Box As RichTextBox, Srch As String)
    37.  
    38. Position = InStr(Position + 1, Box.Text, Srch, vbTextCompare)
    39.  
    40. While Position > 0
    41. Box.SelStart = Position - 1
    42. Box.SelLength = Len(Srch)
    43. Box.SelColor = RGB(255, 0, 0)
    44. found = found + 1
    45. Position = Position + Len(Srch)
    46. Position = InSTr(Position + 1, Box.Text, Srch)
    47. Wend
    48.  
    49. End Function

    Hope that helps

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Throw these lines at the end of the function away:
    VB Code:
    1. 'Now we re-call the function to find the next occurrence of the string
    2.     FindIt = 1 + FindIt(Box, Srch, Start)
    Then you are all set....

    But there is a little extra programming to do first!
    You need the variable Start to be globally available (DIM it at the top of your program, or in a code module), so that you can call FindIt with the Start parameter each time the user clicks on "Find Next".

    Make sure you know what the user wants when clicking on "Find". Do they mean: start where I have got to and look further. Or do they mean: start at the beginning and look from the beginning.

    You may well need a Find and a Find Next button. The FindNext button should [probably] only be visible after the Find button has been clicked. Should it also be available when the user has got to the end of the FindNext loop?

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    LoL, took me 10 minutes to type that, why didnt i copy it??

    I agree with jordan, have a Find and Find Next Button...alot easier..

    Yes, in most applications, Find Next is visible at the same time as Find...

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378
    Hey Guys,
    I can't seem to get this thing to work. Can anybody fix this code and post a working copy? I keep getting errors and as I try to fix one, I get another. What I would like is a code that allows me to search a rich text box and find EVERY occurrence of a string and not miss any. I need it to stop at each find and turn it red. It would be nice to give the total amount of finds at the beginning of the search and then count down as the we go. I know that this is simple for some of YOU guys. It is just hard for me. I will never come close to being a rocket scientist, but I love to program. This stuff fascinates me. Thanks in advanced.
    Last edited by GARY MICHAEL; Dec 23rd, 2002 at 06:05 AM.
    Thanks,
    GARY

  10. #10
    Lively Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    103

    how abour searching back from a database

    Dear all,
    Just wondering what if I would like to seach back my data in the sql database. At present what I did was I have created some forms where the user will enter the information in the textbox and submit the data to be saved in the sql database server. What I want is for the user to be able to search back the data by using a unique key for example. Is that possible? Please advice..thanks in advance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width