aspostrophe in SQL

DentTec

Registered User.
Local time
Today, 09:44
Joined
Apr 3, 2011
Messages
25
My SQL statement was working fine except when the value of criteria has the aspostrophe in it. How do you handle this problem ? Thanks
 
Thanks for the link Jeanette, I'll check it out soon.

Sorry I posted a wrong one. Here is the correct one

Dim sql As String
sql = "SELECT blah blah, [Outstanding Invoices].Client"
sql = sql & " FROM [Outstanding Invoices]"
sql = sql & " GROUP BY blah"
sql = sql & " HAVING ((([Outstanding Invoices].Client) = '" & clientSelected & "'))"
sql = sql & xyz;"

Me.RecordSource = sql


whereas clientSelected is a value selected from a list box in my form

I got an erro when my Client is OTTO'S.
 
Last edited:
It does depend on the circumstances bu double quotes and also cause grief.

One way is to double up the single quotes.
Code:
Replace({expression},"'", "''")

The same can be done with double quotes.

The most reliable way is if the string can be taken directly from a control.

This common usage will fail if an apostrophe is included
Code:
DMax("fieldname", "tablename", "stringfield='" & Me.somecontrol & "'")

But the following will be fine for the same string because Access deals with the datatype automatically and does not require the string delimiters.
Code:
DMax("fieldname", "tablename", "stringfield=Forms!formname.somecontrol")
 
[Outstanding Invoices].Client<>"OTTO'S"

Access is expecting the second single quote to close what it interprets as open single quote.

However you woudn't have a problem if you were to use
[Outstanding Invoices].Client<>Forms!formname.controlname
 
GalaxiomAtHome: I just correct my code, I posted a wrong SQL earlier. I'll take a look at your suggestion tomorrow. Thanks
 
Dim sql As String
sql = "SELECT blah blah, [Outstanding Invoices].Client"
sql = sql & " FROM [Outstanding Invoices]"
sql = sql & " GROUP BY blah"
sql = sql & " HAVING ((([Outstanding Invoices].Client) = """ & clientSelected & """))"
sql = sql & xyz;"
 

Users who are viewing this thread

Back
Top Bottom