Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have been tryng to do an Insert into an Access database but I am getting the error:

SYNTAX ERROR in INSERT INTO statement.

I have tried evreything that I can think of to overcome this error, to no avail. I have run the Insert statements that I have tried in Access itself and everything works just fine. I also have an INSERT/SELECT statement that also fails with

SYNTAX ERROR in From clause.

but runs fine in Access. The table has an AutoNumber field. I ran this against an Access 2007 database, then recreated the table in a new database in Access 2013, neither worked.

What I have tried:

strSql = "INSERT INTO TRANSACTION ([Location], [CoilNo], [HeatNo], [Matsize], [Net], [Gross], [TransDate], [BOL], [PackSlip], [TransType], [Flag], [NewLocation], [CreateDate], [ShipName], [Cost]) VALUES (
@Location, @CoilNo, @HeatNo, @Matsize, @Net, @Gross,
@TransDate, @BOL, @PackSlip, @TransType, @Flag, @NewLocation, @CreateDate, @ShipName, @Cost)"

Try
Using conn As New OleDbConnection(ConnStr)
conn.Open()
Using cmd As New OleDbCommand(strSql, conn)
With cmd
.Parameters.Clear()
.Parameters.AddWithValue("@Location", txtLocation.Text)
.Parameters.AddWithValue("@CoilNo", txtCoilNumber.Text)
.Parameters.AddWithValue("@HeatNo", txtHeatNumber.Text)
........
OR
'cmd.Parameters.Add(New OleDbParameter("@Location", Trim(txtLocation.Text)))
'cmd.Parameters.Add(New OleDbParameter("@CoilNo", Trim(txtCoilNumber.Text)))
'cmd.Parameters.Add(New OleDbParameter("@HeatNo", Trim(txtHeatNumber.Text)))
........
OR
'.Parameters.Add(New OleDb.OleDbParameter("@Location", OleDb.OleDbType.VarChar))
'.Parameters("@Location").Value = txtLocation.Text
'.Parameters.Add(New OleDb.OleDbParameter("@CoilNo", OleDb.OleDbType.VarChar))
'.Parameters("@CoilNo").Value = txtCoilNumber.Text
'.Parameters.Add(New OleDb.OleDbParameter("@HeatNo", OleDb.OleDbType.VarChar))
'.Parameters("@HeatNo").Value = txtHeatNumber.Text
......
End With

Dim recordsAffected As Integer = cmd.ExecuteNonQuery()

End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "SaveConsignedData", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Posted
Updated 26-May-16 10:47am

1 solution

Not enough information or code.

What I can tell you is that Access doesn't used named parameters. You can still assign names to them, but the parameters you add have to appear in the order that they appear in the SQL statement.

Next, every parameter you have is being treated as a string if you're assigning directly from a TextBox. This is bad form as you don't appear to be doing any validation of the data.

Now, if certain columns in your database are numeric, such as CoilNo, you have to convert the text in the TextBox to an integer before you add it as a parameter. You cannot insert a string into a column designed as a number.
 
Share this answer
 
Comments
marla713 27-May-16 12:52pm    
Thank you Dave for your reply. You have great info here and I will adhere to it. I found the problem however. I had to have brackets around the table name, such as [Transaction]. Did this and it worked. I am not use to working with Access. Thank you once again!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900