Solved

Access: how to escape single quotes

Posted on 2009-06-29
6
231 Views
Last Modified: 2013-11-27
Hi x-perts

Here is a simple SQL string:

It obviously returns an error, when a text string contains a quotation mark itself (like dog's)

How can I fox this?

Thanks
SQL = "INSERT INTO Betas (" & strFields & ") VALUES (" & strBetas & ")"
 

strFields = "[field1],[field2]"
 

strBetas = firstNumber & "'" & textValue & "'"
 

textValue = "dog's"

Open in new window

0
Comment
Question by:andy7789
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 total points
ID: 24742089
replace the single quote with 2 singlequotes
textValue = "dog''s"  
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24742107
Aneesh is correct, but you just in case you didn't already notice, you will need to have a comma in strBetas.
SQL = "INSERT INTO Betas (" & strFields & ") VALUES (" & strBetas & ")"

 

strFields = "[field1],[field2]"

 

strBetas = firstNumber & ",'" & textValue & "'"

 

textValue = "dog''s"

Open in new window

0
 

Author Comment

by:andy7789
ID: 24742160
thank you, but it doesn't work

1) coma is just a typo here (not in my actual code)
2) i cannot modify the dog's string, because it is taken as a field value from another table

is there a function to escape quotes like in php?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 350 total points
ID: 24742183

strBetas = firstNumber & ",'" & replace(textValue, "'", "''") & "'"

Open in new window

0
 

Author Comment

by:andy7789
ID: 24742283
works great - thank you!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24742288
You are welcome!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained 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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now