SQL INSERT INTO in Access VBA code module

G

Guest

I am trying to use the data in the variable str as the value to insert into
the field Frequency in the Table tblOutput, I have been banging my head about
this for quite some time and keep geting a varity of errors.

str = "xyz"
strSQL = "INSERT INTO tblOutput (Frequency) VALUES (str)
Set cn = CurrentProject.Connection
cn.Execute strSQL

I can insert it as a literal.

strSQL = "INSERT INTO tblOutput (Frequency) VALUES ('xyz')

Does anyone know the proper syntex for the data in the varible to be written
to the table ???, your help is always appriciated.
 
G

gearybish

David said:
I am trying to use the data in the variable str as the value to insert into
the field Frequency in the Table tblOutput, I have been banging my head about
this for quite some time and keep geting a varity of errors.

str = "xyz"
strSQL = "INSERT INTO tblOutput (Frequency) VALUES (str)
Set cn = CurrentProject.Connection
cn.Execute strSQL

Try

strSQL = "INSERT INTO tblOutput (Frequency) VALUES '" & (str) & "'"
Set cn = CurrentProject.Connection
cn.Execute strSQL
 
G

Geary

If str is string then

strSQL = "INSERT INTO tblOutput (Frequency) VALUES '" & (str) & "'"

that's single quote double quote after VALUES and double single double
after &
 
G

Guest

Thanks Geary,
But somehow I am still getting a runtime error on the cn.Execute strSQL
Syntax error in INSERT INTO statement, do you or anyone else have any other
suggestions
 
D

Douglas J. Steele

It should be:

strSQL = "INSERT INTO tblOutput (Frequency) VALUES ('" & str & "')"

Exagerated for clarity, that's

strSQL = "INSERT INTO tblOutput (Frequency) VALUES ( ' " & str & " ' ) "
 
G

Guest

This syntax worked fine Thank You very much

Douglas J. Steele said:
It should be:

strSQL = "INSERT INTO tblOutput (Frequency) VALUES ('" & str & "')"

Exagerated for clarity, that's

strSQL = "INSERT INTO tblOutput (Frequency) VALUES ( ' " & str & " ' ) "
 

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