Avatar of SteveL13
SteveL13
Flag for United States of America asked on

Syntax issue with not in list code

I'm having an issues when I try to add a string of characters that includes an apostrophe.  Here is the string...
SAM's (Somewhere, IL 61108 - UPS Ground)

It works fine without the apostrophe.

and here is the not in list code:

strSQL = "INSERT INTO tbSamplesTypesLU ([Description],[CustomerID]) VALUES ('" & NewData & "'," & Me.Parent.cboCustomerID & ");"

Can someone spot my problem?
Microsoft Access

Avatar of undefined
Last Comment
Fabrice Lambert

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
aikimark

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Bill Prew

For the fields that can contain an apostrophe, just do a replace when you add them to the query and replace a single quote with two single quotes, like:

Replace(dataVariable,"'","''")

Open in new window


»bp
Fabrice Lambert

Nope, it won't work either, because your data can hold simple quotes, double quote or any mix of both.

You should use a parameterized query instead.
Dim sql As String
sql = vbNullString
sql = sql & "PARAMATERS paramDescription Text(255)," & vbcrlf
sql = sql & "    paramCustomerID Long;" & vbcrlf
sql = sql & "INSERT INTO tbSamplesTypesLU (" & vbcrlf
sql = sql & "    Description,"  & vbcrlf
sql = sql & "    CustomerID)"  &vbcrlf
sql = sql & "VALUES (" & vbcrlf
sql = sql & "    [paramDescription]," & vbcrlf
sql = sql & "    {paramCustomerID]);"

Dim db As DAO.Database
Set db = currentDb

Dim qd As DAO.QueryDef
Set qd = db.CreateQueryDef("tempQD", sql)

qd.parameters("paramDescription").value = NewData
qd.parameters("paramCustomerID").value = CLng(Me.Parent.cboCustomerID)
qd.Execute dbfailonError
qd.Close

db.queryDefs.delete(qd.name)

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck