Search code examples
vb.netms-accessselectoledbexception

OleDbException:Syntax error (missing operator) in query expression '@bottlecount ORDER BY ID DESC'


I got this error upon testing on how to update the particular column in the latest database entry by clicking a button. Before, i set the update statement as

 Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount"
 statement &=" WHERE room_number =1" 

But all entries in the database are updated, thats why Im trying to use ORDER BY ID DESC, ID is the primary id. The program should get the latest database entry and it will only update the bottle_used. Upon using it, I got an OleDbException.

This is my code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '====test number of bottles=============
   Form5.lblBottle.Text = Form5.lblBottle.Text + 1

    Dim statement As String = "UPDATE tblPatientInfo SET bottle_used = @bottlecount ORDER BY ID DESC"

     Dim cmd As New OleDbCommand

    With cmd

        .Connection = Conn
        .CommandType = CommandType.Text
        .CommandText = statement
        .Parameters.AddWithValue("@bottlecount", Form5.lblBottle.Text)

        Conn.Open()
        .ExecuteNonQuery()
    End With
    Conn.Close()

End Sub

Any help would be appreciated. thanks!


Solution

  • It seems you want to update the row with the maximum ID value. The DMax function should make this easy.

    UPDATE tblPatientInfo
    SET bottle_used = @bottlecount
    WHERE ID = DMax('ID', 'tblPatientInfo')