Solved

Single quote ! (Again)

Posted on 2011-03-10
11
408 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 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
 
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

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.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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 …
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…

803 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