Question

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!

Was it helpful?

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')

OTHER TIPS

basically you cannot add an ORDER BY clause directly in an update statement. The only way you can to create an update statement with order by is to create a select subquery with order by clause. Example,

UPDATE  messages 
SET status=10 WHERE ID in 
   (
    SELECT ...
    FROM ... 
    WHERE ... 
    ORDER BY priority
   )

maybe you want this,

UPDATE tblPatientInfo 
SET bottle_used = @bottlecount
WHERE ID =
    (
        SELECT MAX(ID) maxID
        FROM tblPatientInfo
        WHERE room_number = 1
    )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top