Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting "Syntax error in INSERT INTO Statement" error

Status
Not open for further replies.

mjschroed

MIS
Jun 19, 2001
8
US
I have created the following piece of code to update a database (I am kind of new at this) but it fails with the above error. If I put the insert statement into a SQL window with the right values - it works fine. Here is the statement:

Dim UserProjName As String
Dim DRSiRecordType As Long
Dim DRSiResultNumber As Long
Dim DRSsDescription As String
Dim Year As Long
Dim Month As Long
Dim Amount As Long

Bunch of other stuff.....

sSql = "INSERT INTO Occupancy ( UserProjName, DRSiRecordType, DRSiResultNumber, DRSsDescription, Year, Month, Amount ) values (""" & UserProjName & """," & DRSiRecordType & "," & DRSiResultNumber & ",""" & DRSsDescription & """," & Year & "," & Month & "," & Amount & ")"
l_cmdExecuteSQL.ActiveConnection = CurrentProject.Connection
l_cmdExecuteSQL.CommandText = sSql
l_cmdExecuteSQL.Execute

From the debugger window - it is building this query:

"INSERT INTO Occupancy ( UserProjName, DRSiRecordType, DRSiResultNumber, DRSsDescription, Year, Month, Amount ) values ("135",10000,6,"Mall Shop GLA Occ. Area SqFt",2002,1,286316)"

Thanks in advance,
Melissa
 
Your string values need to be surrounded by single quote (') not double quotes ("). Try this,

"INSERT INTO Occupancy ( UserProjName, DRSiRecordType, DRSiResultNumber, DRSsDescription, Year, Month, Amount ) values ('" & UserProjName & "'," & DRSiRecordType & "," & DRSiResultNumber & ",'" & DRSsDescription & "'," & Year & "," & Month & "," & Amount & ")"


Thanks and Good Luck!

zemp
 
I just tried that but got the same error. Thanks for the suggestion though!

"INSERT INTO Occupancy ( UserProjName, DRSiRecordType, DRSiResultNumber, DRSsDescription, Year, Month, Amount ) values ('135',10000,6,'Mall Shop GLA Occ. Area SqFt',2002,1,286316)"

Melissa

 
I figured it out. Turns out Month and Year have system values (?) so they have to be enclosed in [].

This worked:

INSERT INTO Occupancy ( UserProjName, DRSiRecordType, DRSiResultNumber, DRSsDescription, [Year], [Month], Amount ) values ('" & UserProjName & "'," & DRSiRecordType & "," & DRSiResultNumber & ",'" & DRSsDescription & "'," & Year & "," & Month & "," & Amount & ")

Thanks!
 
Yep, seems pretty logical, they're build in functions....


Greetings,
Rick
 
Yes, you have to watch out for Key words that the system wants to interpret as somrthing else. Good catch.

Thanks and Good Luck!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top