Search code examples
vb.netms-accessoledb

Error: Syntax error in UPDATE statement


the error i get while executing the code below in OleDb

        Try
            con.Open()
            Dim cmd As New OleDbCommand("Select * from customer", con)
            cmd.CommandText = " update customer set hr =@hr,min =@min "
            cmd.Parameters.AddWithValue("@hr", ComboBoxHr.SelectedIndex.ToString())
            cmd.Parameters.AddWithValue("@min", ComboBoxMin.SelectedIndex.ToString())
            cmd.ExecuteNonQuery()
            TwoDigit(ComboBoxHr)
            MessageBox.Show("CONRATULATIONS! ...Click the Start button to see the changes")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

Which works fine if i remove the "min" part. What could be the reason?


Solution

  • I believe AakashM is right. You can however use a keyword as a column name as long as you put it in [] like

    Try
            con.Open()
            Dim cmd As New OleDbCommand("Select * from customer", con)
            cmd.CommandText = " update customer set hr =@hr,[min] =@min "
            cmd.Parameters.AddWithValue("@hr", ComboBoxHr.SelectedIndex.ToString())
            cmd.Parameters.AddWithValue("@min", ComboBoxMin.SelectedIndex.ToString())
            cmd.ExecuteNonQuery()
            TwoDigit(ComboBoxHr)
            MessageBox.Show("CONRATULATIONS! ...Click the Start button to see the changes")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try