如何修复Access VBA中UPDATE语句中的语法错误

时间:2019-06-18 19:18:55

标签: sql access-vba

我正在尝试建立数据库,并在UPDATE语句中遇到语法问题。有人知道语句有什么问题吗?

我尝试调整单引号和双引号的用法,但仍然无法正常工作。

    CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
    & " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
    Else
    CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
    & " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
    & " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "', WHERE ContactID = '" & Me.txtContactID & "'"
    End If
    Exit Sub

我不断收到3144语法错误,该错误未提供任何原因的提示。任何帮助表示赞赏。

更新:感谢Jerry M.和forpas建议在何处删除逗号。这有帮助,但我没有收到错误3061,“参数太少。应该为1”。 我将发布一个新问题。

1 个答案:

答案 0 :(得分:0)

正如评论中提到的@forpas一样,您似乎多了,

尝试以下方法:

CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
& " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
Else
CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
& " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
& " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "'WHERE ContactID = '" & Me.txtContactID & "'"
End If
Exit Sub ```