0

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

3 Answers 3

2

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(?,?,?,?,?,?,?)"
Sign up to request clarification or add additional context in comments.

Comments

0
`cmdText = "insert into tblBottling([bottlingDate],[workerName],[seed],[size],[noOfBottles],[timeTaken],[remarks]) values(?,?,?,?,?,?,?)"

Please always use square brackets for your columns incase of such error.

Because you cannot memorise all the keywords.

Good Luck

Comments

0

try to change the columns names when you create tables avoiding keywords like username like i did 😅

1 Comment

Please add in some details about the what. Consider a reference to a list of reserved names and keywords

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.