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
Solved

VBA Replace Values

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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