We help IT Professionals succeed at work.

Syntax issue with not in list code

SteveL13
SteveL13 asked
on
64 Views
Last Modified: 2019-02-05
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?
Comment
Watch Question

Social distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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 LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions