I am trying to insert a record into MS Access db, with the below code. I have used the same type of code several times in my project. But I dont know why it is erroring , saying that there is a syntax error. Some one please suggest me where the code is wrong.
Try
If MainForm.con.State = ConnectionState.Closed Then
MainForm.con.Open()
End If
Dim cmdText As String
cmdText = "insert into tblBottling(bottlingDate,workerName,seed,size,noOfBottles,timeTaken,remarks) values(?,?,?,?,?,?,?)"
Dim command As OleDbCommand = New OleDbCommand(cmdText, MainForm.con)
command.Parameters.AddWithValue("@bottlingDate", botDate.Value.ToString("dd-MM-yy"))
command.Parameters.AddWithValue("@workerName", workerCB.SelectedItem.ToString)
command.Parameters.AddWithValue("@seed", seedCB.SelectedItem.ToString)
command.Parameters.AddWithValue("@size", botSizeCB.SelectedItem.ToString)
command.Parameters.AddWithValue("@noOfBottles", CInt(noOfBot.Text))
command.Parameters.AddWithValue("@timeTaken", timeTakenTxt.Text)
command.Parameters.AddWithValue("@remarks", remarksTxt.Text)
command.ExecuteNonQuery()
MainForm.con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Size is a reserved keyword for MS-Access. If you want to use that word as column name then you should always enclose it between square brackets
cmdText = "insert into tblBottling
(bottlingDate,workerName,seed,[size],noOfBottles,timeTaken,remarks)
values(?,?,?,?,?,?,?)"