Solved

Find/Replace?

Posted on 1998-11-12
4
173 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
ID: 1497633

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
ID: 1497634

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
ID: 1497635
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
ID: 1497636
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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