Solved

VBA Replace Values

Posted on 2007-11-21
4
1,445 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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