Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Function to do Find/Replace in records

Status
Not open for further replies.

HFloyd

Programmer
Jun 5, 2002
71
US
Hi,

I was interested in automating "cleanup" of data imported into a database. I was looking for a way to do find/replace operations from VBA code, or preferably, update queries.

On this site, I found some code in a post by kramerica (Mar 7, 2002), which worked well, except for not handling Null values very effectivly. I did some minor re-writing and documenting and now offer it up to anyone who has been looking for a similar function. This is very easy to use in an update query.
Code:
Function FindReplace(strOrig As Variant, strOld As String, strNew As String)
'Function to search and replace characters in a string
'-----------------------------------------------------------
'   ARGUEMENT               DESCRIPTION
'   --------------------------------------------------------
'   strOrig                 String in which to
'                           search/replace.
'
'   strOld                  String you are searching for.
'
'   strNew                  String to replace the searched 
'                           for string.
'   --------------------------------------------------------
'   EXAMPLE
'   --------------------------------------------------------
'   MyString = "555.318.6755"
'   MyNewString = FindReplace(MyString,".","-")
'   (MyNewString is now "555-318-6755")
'-----------------------------------------------------------

Dim intAt As Integer, strAltered As String

'Check for arguements
    If IsNull(strOld) Or IsNull(strNew) Then
        FindReplace = "ERROR! CHECK ARGUEMENTS!"
        Exit Function
    End If
    
'Check for null string
    If IsNull(strOrig) Then
        FindReplace = strOrig
        Exit Function
    End If
   
'Do function
    For intAt = 1 To Len(strOrig)
        If Mid(strOrig, intAt, Len(strOld)) = strOld Then
            strAltered = strAltered & strNew
            intAt = intAt + (Len(strOld) - 1)
        Else
            strAltered = strAltered & Mid(strOrig, intAt, 1)
        End If
    Next intAt
    FindReplace = strAltered

End Function

 
Oops!

There was a tiny error in the previously posted code. The corrected code is as follows:

Code:
Function FindReplace(strOrig As Variant, strOld As String, strNew As String)
'Function to search and replace characters in a string
'-----------------------------------------------------------
'   ARGUEMENT               DESCRIPTION
'   --------------------------------------------------------
'   strOrig                 String in which to
'                           search/replace.
'
'   strOld                  String you are searching for.
'
'   strNew                  String to replace the searched 
'                           for string.
'   --------------------------------------------------------
'   EXAMPLE
'   --------------------------------------------------------
'   MyString = "555.318.6755"
'   MyNewString = FindReplace(MyString,".","-")
'   (MyNewString is now "555-318-6755")
'-----------------------------------------------------------

Dim intAt As Integer, strAltered As String

'Check for arguements
    If IsNull(strOld) Or IsNull(strNew) Then
        FindReplace = "ERROR! CHECK ARGUEMENTS!"
        Exit Function
    End If
    
'Check for null string
    If IsNull(strOrig) Then
        FindReplace = strOrig
        Exit Function
    End If
   
'Do function
    For intAt = 1 To Len(strOrig)
        If Mid(strOrig, intAt, Len(strOld)) = strOld Then
            strAltered = strAltered & strNew
            intAt = intAt + (Len(strOld) - 1)
        Else
            strAltered = strAltered & Mid(strOrig, intAt, 1)
        End If
    Next intAt
    FindReplace = strAltered

End Function


Sorry for any inconveience!


 
Hi'

would you give me Example with a form

thanks alot
 
kh5455,

I don't understand what you need, do you mean that you want to use it to display field text a certain way, but not actually change the data?

Thanks,

Heather

[yinyang] Floyd Innovations [yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top