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

Single quote ! (Again)

Posted on 2011-03-10
11
409 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 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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 …

809 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