Solved

Find/Replace?

Posted on 1998-11-12
4
176 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 64
vb6 connector to SQL Server 2 42
passing a value with stream reader AFTER a ";" 3 92
VB6 - Convert HH:MM into Decimal 8 75
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

739 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