Solved

Find/Replace?

Posted on 1998-11-12
4
171 Views
Last Modified: 2013-12-25
I am looking for the best way to go about a Find/Replace Function in VB to Find and Replace words in textBoxes and selected items of combo and listboxes with  newly defined words.
0
Comment
Question by:khagen
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
hddp666 earned 20 total points
Comment Utility

Private Sub WordReplace()
dim i, j, k As Long ' Use long to accomodate large text strings
dim strText As String ' contents of textbox
dim strNew As String ' the new word to replace strOld
dim strOld As String ' the old word to find in strText
dim strFront As String ' the text string up to the old word
dim strBack As String ' the text string after the old word

strText = FormX.TextBox.Text

If strOld = "" or strOld = " " Then Exit Sub

Do

' Find strOld; not just those letters within another word
i = InStr$(strText, " " & strOld & " ")

' Find strOld at the end of a sentence
j = InStr$(strText, " " & strOld & ".")

' Find Capitalized strOld at the beginning of a sentence
k = InStr$(strText, " " & UCase(Left$(strOld, 1) & _
Right$(strOld, Len(strOld) - 1) & " ")

'Repeat this scheme for all uppercase, within parentheses, _ hyphenated, &c., as needed.

' Replace strOld with strNew

If i > 0 Then
strFront = Left$(strText, i - 1)
strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
strText = strFront & " " & strNew & " " & strBack
End If

If j > 0 Then
strFront = Left$(strText, j - 1)
strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
strText = strFront & " " & strNew & "." & strBack
End If

If k > 0 Then
strFront = Left$(strText, k - 1)
strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
strText = strFront & " " & UCase(Left$(strNew, 1) & _
Right$(strNew, Len(strNew) - 1) & " " & strBack
End If


If i = 0 And j = 0 And k = 0 Then Exit Do
' No more strOld to find

Loop ' Go back and find more strOld words to change.

' Put the updated string in the textbox
FormX.TextBox.Text = strText

' Put strNew as the selected Combo item
FormX.Combo1.Text = strNew

End Sub
0
 
LVL 1

Expert Comment

by:hddp666
Comment Utility

I JUST NOTICED THAT RIGHT AFTER YOU GET "i", YOU SHOULD DO THE SWAP. THEN, GET "j" AND DO THAT SWAP. AND SO FORTH.
THE CODE BELOW IS BETTER.


    Private Sub WordReplace()
    dim i, j, k As Long ' Use long to accomodate large text strings
    dim strText As String ' contents of textbox
    dim strNew As String ' the new word to replace strOld
    dim strOld As String ' the old word to find in strText
    dim strFront As String ' the text string up to the old word
    dim strBack As String ' the text string after the old word

    strText = FormX.TextBox.Text

    If strOld = "" or strOld = " " Then Exit Sub


    Do

    ' Replace strOld with strNew

    ' Find strOld; not just those letters within another word
    i = InStr$(strText, " " & strOld & " ")
    If i > 0 Then
    strFront = Left$(strText, i - 1)
    strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
    strText = strFront & " " & strNew & " " & strBack
    End If

    ' Find strOld at the end of a sentence
    j = InStr$(strText, " " & strOld & ".")
    If j > 0 Then
    strFront = Left$(strText, j - 1)
    strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
    strText = strFront & " " & strNew & "." & strBack
    End If

    ' Find Capitalized strOld at the beginning of a sentence
    k = InStr$(strText, " " & UCase(Left$(strOld, 1) & _
    Right$(strOld, Len(strOld) - 1) & " ")
    If k > 0 Then
    strFront = Left$(strText, k - 1)
    strBack = Right$(strText, Len(strText) - i - Len(strOld) - 2)
    strText = strFront & " " & UCase(Left$(strNew, 1) & _
    Right$(strNew, Len(strNew) - 1) & " " & strBack
    End If

    'Repeat this scheme for all uppercase, within parentheses, _ hyphenated, &c., as needed.

    If i = 0 And j = 0 And k = 0 Then Exit Do
    ' No more strOld to find

    Loop ' Go back and find more strOld words to change.

    ' Put the updated string in the textbox
    FormX.TextBox.Text = strText

    ' Put strNew as the selected Combo item
    FormX.Combo1.Text = strNew

    End Sub
0
 
LVL 1

Author Comment

by:khagen
Comment Utility
I am having a bit  of trouble inserting the code and adapting it to my application.  Where do I put it exactly(mdiForm, or each individual.  I am using a MDIForm now, and need a short example as to how to the code.  IE do i put new text boxes on each form, a new Form all together, ect.  Thanks Keith
0
 
LVL 1

Expert Comment

by:hddp666
Comment Utility
This is a simple example of a complete program that searches a textbox's contents for a word, and replaces it with another word. A button is used to effect the change.

Name an SDI form, or MDI child, as Form1. Put two small text boxes (Form1.Text2 and Form1.Text3) and a button (Form1.Command1) at the top. Put a large text box (multi-line and vertical scrollbar; max length = 64000) at the bottom.

Text2 = old word
Text3 = new word

Put the following code into the GENERAL declarations of the form. This code has some corrections to the above coding that became apparent when I ran it. Compare them and see if you can find the problems.

Notice that the sub WordReplace is not Private, so that it may be called from other forms, such as a menu click on an MDI parent. This sub would actually be best located in a module, rather than in a form's declarations.



Private Sub Command1_Click()
Dim strOld As String
Dim strNew As String
' get the words from the small text boxes
strOld = Form1.Text2.Text
strNew = Form1.Text3.Text
If strOld = "" Or strNew = "" Then Exit Sub
' call the sub WordReplace with the two arguments
WordReplace strOld, strNew
End Sub

Sub WordReplace(strOld As String, strNew As String)
Dim i, j, k As Long ' Use long to accomodate large text strings
Dim strText As String ' contents of textbox
Dim strFront As String ' the text string up to the old word
Dim strBack As String ' the text string after the old word

strText = Form1.Text1.Text

If strOld = "" Or strOld = " " Then Exit Sub


Do

    ' Replace strOld with strNew

    ' Find strOld; not just those letters within another word
    i = InStr(strText, " " & strOld & " ")
    If i > 0 Then
        strFront = Left$(strText, i - 1)
        strBack = Right$(strText, Len(strText) - i - Len(strOld) - 1)
        strText = strFront & " " & strNew & " " & strBack
    End If

    ' Find strOld at the end of a sentence
    j = InStr(strText, " " & strOld & ".")
    If j > 0 Then
        strFront = Left$(strText, j - 1)
        strBack = Right$(strText, Len(strText) - j - Len(strOld) - 1)
        strText = strFront & " " & strNew & "." & strBack
    End If

    ' Find Capitalized strOld at the beginning of a sentence
    k = InStr(strText, UCase(Left$(strOld, 1)) & Right$(strOld, Len(strOld) - 1) & " ")
    If k > 0 Then
        strFront = Left$(strText, k - 1)
        strBack = Right$(strText, Len(strText) - k - Len(strOld))
        strText = strFront & " " & UCase(Left$(strNew, 1)) & Right$(strNew, Len(strNew) - 1) & " " & strBack
    End If
   
    'Repeat this scheme for all uppercase, within parentheses, _ hyphenated, &c., as needed.

    If i = 0 And j = 0 And k = 0 Then Exit Do
    ' No more strOld to find

Loop ' Go back and find more strOld words to change.

' Put the updated string in the textbox
Form1.Text1.Text = strText

' Put strNew as the selected Combo item
'Form1.Combo1.Text = strNew

End Sub



0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now