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

Access: how to escape single quotes

Posted on 2009-06-29
6
234 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
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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

861 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