Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Find/Replace?

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
khagen
Asked:
khagen
  • 3
1 Solution
 
hddp666Commented:

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
 
hddp666Commented:

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
 
khagenAuthor Commented:
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
 
hddp666Commented:
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

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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now