简体   繁体   中英

Run-time error on SQL UPDATE in VBA

I am trying to make some code, which updates a row, if the item already exists. It looks like this:

updateStr = "UPDATE platinum_paste SET ([aluminium], [calcium], [chrome], [iron], [lead], [silicon], [zirconium]) VALUES (" & aluminium & "," & _
                                                                                                                              calcium & "," & _
                                                                                                                              chrome & "," & _
                                                                                                                              iron & "," & _
                                                                                                                              lead & "," & _
                                                                                                                              silicon & "," & _
                                                                                                                              zirconium & _
                                                                                                                              ") WHERE [lot_number] = " & lotNumber

Debug.Print updateStr

If MsgBox("Item already exists. Do you want to update its details?", vbYesNo) = vbYes Then
            objMyConn.Execute updateStr, dbFailOnError

Else
            'Do nothing

End If

When I try to run it, clicking "Yes" in the MsgBox will result in an error:

Run-time error '-2147217900 (80040e14)':

Automation error

Can anyone tell me what I am doing wrong?I have tried to add all possible debug functions I can think of. The database connection works fine for SELECT and INSERT . This is my INSERT string, which works just fine:

insertStr = "INSERT INTO platinum_paste ([lot_number], [aluminium], [calcium], [chrome], [iron], [lead], [silicon], [zirconium]) VALUES (" & lotNumber & "," & _
                                                                                                                                                 aluminium & "," & _
                                                                                                                                                 calcium & "," & _
                                                                                                                                                 chrome & "," & _
                                                                                                                                                 iron & "," & _
                                                                                                                                                 lead & "," & _
                                                                                                                                                 silicon & "," & _
                                                                                                                                                 zirconium & ")"

The Syntax of your UPDATE -String is wrong. You need to write

 UPDATE table_name
 SET column1=value1,column2=value2,...
 WHERE some_column=some_value;

For further information, have a look at http://www.w3schools.com/sql/sql_update.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM