Solved

Single quote ! (Again)

Posted on 2011-03-10
11
397 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
  • 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 119

Expert Comment

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

Assisted Solution

by:Rey Obrero
Rey Obrero earned 125 total points
ID: 35103056
try

CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,auditDate) values (" & Chr(34) & AuditDetails & chr(34) & ",#" & Now() & " #)"
0
 
LVL 38

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 38

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

14 Experts available now in Live!

Get 1:1 Help Now