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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If your data might contain an apostrophe char, you should do this:
strSQL = "INSERT INTO tbSamplesTypesLU ([Description],[CustomerID]) VALUES (""" & NewData & """," & Me.Parent.cboCustomerID & ");"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
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:


Open in new window

Fabrice LambertConsultingCommented:
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


Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.