Solved

Find/Replace?

Posted on 1998-11-12
4
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

623 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