0

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 & ")"
7
  • You should use parameterized queries instead of dumping user input directly in the query string. That will probably fix your problem. Commented Oct 22, 2015 at 11:54
  • I will try to look into that, however it still does not explain why my method does not work... It is almost identical to my INSERT string, which works just fine. Commented Oct 22, 2015 at 11:57
  • When you say it is 'almost identical' what are the differences? Commented Oct 22, 2015 at 12:00
  • Added to OP for comparison. Commented Oct 22, 2015 at 12:01
  • your update syntax looks weird. you are sure thats correct? Commented Oct 22, 2015 at 12:03

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, seems obvious now. Clearly, I need more coffee.
Thanks, that worked. Not used to working much with UPDATE in SQL. I still don't get why none of my Debug attempts gave me anything useful...?
The Debug.Print-Messages I see in your code just print out the SQL String. They can't test its usefullness...

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.