Special characters INSERT INTO command

G

Guest

To insert Form data into the database I use the following command on the
button:

CurrentDb.Execute ("INSERT INTO TableName ([FieldName]) VALUES ('" & cboName
& "') "), dbFailOnError

When I put a single quote in the ComboBox, it returns a RunTime error 3075
(Syntax Error). Does anybody know how to correct this?

Thanks in advance!
Jochem
 
A

Allen Browne

Use double-quote characters around the value in the string.
And double-them up to VBA understands it is not the end of the string.

strSql = "INSERT INTO TableName ([FieldName]) VALUES (""" & cboName & """);"
 
S

Stefan Hoffmann

hi Jochem,

Jochem said:
To insert Form data into the database I use the following command on the
button:

CurrentDb.Execute ("INSERT INTO TableName ([FieldName]) VALUES ('" & cboName
& "') "), dbFailOnError

When I put a single quote in the ComboBox, it returns a RunTime error 3075
(Syntax Error). Does anybody know how to correct this?
You are trying to Excute this
"INSERT INTO TableName ([FieldName]) VALUES (''')"

use Replace to escape the single quote:

Dim SQL As String

SQL = Replace(cboName.Value, "'", "''")
SQL = "INSERT INTO TableName ([FieldName]) " & _
"VALUES ('" & SQL & "') ")

CurrentDb.Execute SQL, dbFailOnError


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top