Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access: how to escape single quotes

Posted on 2009-06-29
6
Medium Priority
?
240 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 600 total points
ID: 24742089
replace the single quote with 2 singlequotes
textValue = "dog''s"  
0
 
LVL 60

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1400 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 60

Expert Comment

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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

963 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