Solved

Single quote ! (Again)

Posted on 2011-03-10
11
415 Views
Last Modified: 2012-05-11
My audit trail in Access contains the line below.

It works fine until there is an single quote in the BuildStr and then it fails.

How do I amend the "BuildStr=" code below to cater for the single quote.

Syntax error (missing operator) in query expression ''** Address1 Changed From: 17 Main Street , NewYork    To: 17 Main Street , NewYork O'Brien


(Note the surname O'Brien has a single quote)
BuildStr = "** " & ccont.Name & " Changed From: " & ccont.OldValue & "    To: " & ccont.Value & Chr(13) & Chr(10

Open in new window

0
Comment
Question by:Patrick O'Dea
[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
  • 2
  • 2
  • +3
11 Comments
 

Author Comment

by:Patrick O'Dea
ID: 35102996
Actually this line is probably the cause of my error.
It crashes when the is a single quote in my string (e.g. the name O'Brien)


CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,auditDate) values ( '" & AuditDetails & "',#" & Now() & " #)"
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35103014
BuildStr = "** " & ccont.Name & " Changed From: " & ccont.OldValue & "    To: " & chr(34) & ccont.Value & chr(34) & Chr(13) & Chr(10
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 125 total points
ID: 35103056
try

CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,auditDate) values (" & Chr(34) & AuditDetails & chr(34) & ",#" & Now() & " #)"
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 125 total points
ID: 35103321
Or just wrap Your input with Replace(auditdetails,"'","''")

Doubling up the single quotes is what I've found to work well
0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 125 total points
ID: 35103354
You original code is fine except that you need to "escape" all single quotes in the input string before you construct the command string.

Single quotes embedded in a value need to be escaped by doubling them (two single quotes in a row means one single quote).

I think this can best be done with the replace function.  Before using it in the line you're having trouble with, create a temp variable from AuditDetails by changing all single quotes ' into two single quotes '' and use that variable in your command string.

Unless you escape (double) your single quotes, the final command string, no matter how it is constructed, will not be valid.
0
 
LVL 75
ID: 35103455
"It works fine until there is an single quote in the BuildStr and then it fails."
Exactly.  Why is why ... contrary to popular belief ... Singe Quotes are problematic when used in Access criteria expressions - for exactly that reason.  Use the Chr(34) ... Double Quote ... as shown above ... to wrap Text values.

mx
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35103737
Chr(34) works fine for single quotes but shifts the problem to embedded double quotes which will still cause a parse error.

Handling single quotes but not double quotes may be fine for this application, in which case go with the Chr(34) approach as it is more efficient.

If you want to be able to have either single or double quotes in the string without failing, then one or the other of them will need to be escaped (doubled).
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
ID: 35107753
"Singe Quotes are problematic when used in Access criteria expressions "

Can you elaborate on this? I've used the Replace method for many years and have not had trouble with criteria expressions, as long as I escape the single quote.

0
 
LVL 75
ID: 35109745
I already elaborated on it above.

mx
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 35109998
Thanks all.

(I do find it amazing that the Microsofts of theis world have not found a better way to handle this matter!)
0
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35110266
The main annoying thing I find is the lack of a common escape character. Sometimes its Backslash sometimes it's to double up the quote sometimes it's square brackets, etc... As Lin as you know you are looking for the escape character google finds your answer pretty quick.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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 …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

730 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