Solved

Single quote ! (Again)

Posted on 2011-03-10
11
421 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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.
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…

729 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