Solved

VBA Replace Values

Posted on 2007-11-21
4
1,413 Views
Last Modified: 2012-08-13
Hi,
Can someone tell me how I would replace a ' in my text field. If I try to update the field it screws up my query! The code I thought I mite use would be something like; (strMessage being my text field name)

         If InStr(strMessage, "'") = 1 Then
                Me("strMessage").Value = Replace(strMessage, "'", " ")
         End If

Woudl this work?
0
Comment
Question by:andyb7901
  • 2
4 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20327666

If InStr(me("strMessage"), "'") =1 Then
                Me("strMessage").Value = Replace(Me("strMessage"), "'", " ")
        End If
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20327722
Replace ' single quote with 2 single quote and all would be fine.
 Me("strMessage").Value = Replace(strMessage, "'", "''")
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20327745
what value do you want as replacement?

0
 
LVL 18

Accepted Solution

by:
Sham Haque earned 500 total points
ID: 20328169
try this:

changes to above are:

Instr ... > 0 - rather than =1. this is the position of the erroneous char you're looking for - assuming that it's not always the first character - that would be simpler to deal with(!)

Also, using Chr(39) rather than escaping a [']

dim strMessage as String
strMessage = me.MyField.Value
 
If InStr(strMessage), Chr(39)) > 0 Then
               strMessage = Replace(strMessage, chr(39), " ")
End If

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now