0

Can someone take a look a the stSQL string and help me fix the syntax error I am getting associated with the UPDATE statement?

Run-time error '-2147217900 (8004e14)': Syntax error in UPDATE statement.

I have a rudimentary understanding of SQL and don't seem to understand where I have gone wrong.

I want to update the fields of Table 1 if the FileName UserForm value matches a FileName field in the Access Db.

Thanks

Public Sub UpdateDatabaseEntry()

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim stDB As String, stSQL As String, stProvider As String
Dim FileName As String
Dim Nickname As String
Dim RecipientName As String
Dim RecipientRelationship As String
Dim Summary As String
Dim Noteworthy As String
Dim PreparedBy As String

FileName = UserForm1.FileNameTextBox.Text
Nickname = UserForm1.NicknameTextBox.Text
RecipientName = UserForm1.RecipientNameTextBox.Text
RecipientRelationship = UserForm1.RecipientRelationshipComboBox.Text
Summary = UserForm1.SummaryTextBox.Text
Noteworthy = UserForm1.NoteworthyCheckBox.Value
PreparedBy = UserForm1.PreparedByTextBox.Text

stDB = "Data Source= E:\MyDb.accdb"
stProvider = "Microsoft.ACE.OLEDB.12.0"

//Opening connection to database
With cn
    .ConnectionString = stDB
    .Provider = stProvider
    .Open
End With

//SQL Statement telling database what to do
stSQL = "UPDATE Table1" & _
        "SET Nickname= '" & Nickname & "', RecipientName= '" & RecipientName & "', " & _
            "RecipientRelationship= '" & RecipientRelationship & "', Summary= '" & Summary & "', " & _
            "Noteworthy= '" & Noteworthy & "', PreparedBy= '" & PreparedBy & "', " & _
        "WHERE FileName= '" & FileName & "'"

cn.Execute stSQL

cn.Close
Set rs = Nothing
Set cn = Nothing

End Sub
1
  • In general, when you have a problem like this, just do a Debug.Print stSQL right before you execute the SQL. That way you see in the Immediate window exactly what the SQL is, and that helps you find any syntax errors. Commented Feb 16, 2014 at 21:53

1 Answer 1

2

At least one problem is caused by lack of spaces in the query. So your query started UPDATE Table1set.

stSQL = "UPDATE Table1 " & _
        "SET Nickname= '" & Nickname & "', RecipientName= '" & RecipientName & "', " & _
            "RecipientRelationship= '" & RecipientRelationship & "', Summary= '" & Summary & "', " & _
            "Noteworthy= '" & Noteworthy & "', PreparedBy= '" & PreparedBy & "'" & _
        "WHERE FileName= '" & FileName & "'"

If this doesn't fix the problem. Then edit your question with the value of stSQL after the variable substitution.

EDIT:

As TS points out, another problem is the , before the where (fixed above).

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

3 Comments

@T.S. . . . Yes, that would count as another problem.
Thanks! The comma after the , before the WHERE was really my issue. Everything works!
@T.S. . . . You should put the comment into an answer so the OP can accept it.

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.