ADD / UPDATE BUTTON语法错误

时间:2016-05-25 13:43:27

标签: vba command ms-access-2013

我目前在MS Access 2013中编写了我的添加/更新按钮,但由于一些奇怪的原因,我无法尝试使其工作,它告诉我的是我在UPDATE语句中有'语法错误'...这里是我的整体代码:

    Private Sub cmdAdd_Click()
    'In the button add we have two options
    '1. Insert
    '2. Update
    If Me.txtID.Tag & "" = "" Then

    CurrentDb.Execute "INSERT INTO tblClients ( ClientID, ClientName, Gender, " & _
                             "City, [Address (Fisical)], [Cellphone/Telephone] ) " & _
                                "SELECT " & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "', '" & Me.cboCity & "','" & Me.txtAddress & "','" & Me.txtCellphone & "'"
    Else
    'Otherwise the data will be updated
    CurrentDb.Execute "UPDATE tblClients" & _
    "SET ClientID =" & Me.txtID & _
    ", ClientName='" & Me.txtName & "'" & _
    ", Gender='" & Me.cboGender & "'" & _
    ", City='" & Me.cboCity & "'" & _
    ", Cellphone/Telephone='" & Me.txtCellphone & "'" & _
    ", Address (Fisical) ='" & Me.txtAddress & "'" & _
    "WHERE ClientID =" & Me.txtID.Tag


    End If


   cmdClear_Click

   tblClients_subform.Form.Requery


   End Sub

请提供帮助

1 个答案:

答案 0 :(得分:0)

看起来很近,以这种方式想起来,因为我知道它会在发生时让我感到沮丧。如果错误说的是语法错误,它不会说谎,而且对我来说往往不能看到木头的树木。你错过了几个空格: -

CurrentDb.Execute "UPDATE [tblClients]" & _
" SET [ClientID] =" & Me.txtID & _
", [ClientName]='" & Me.txtName & "'" & _
", [Gender]='" & Me.cboGender & "'" & _
", [City]='" & Me.cboCity & "'" & _
", [Cellphone/Telephone]='" & Me.txtCellphone & "'" & _
", [Address (Fisical)] ='" & Me.txtAddress & "'" & _
" WHERE [ClientID] =" & Me.txtID.Tag
  • SET ClientID...
  • 之前的空格
  • WHERE ClientID
  • 之前的空格
  • 括号栏和表格名称

这也是我学到的一个技巧: -

  1. 打开新查询并查看SQL
  2. 将已执行的字符串粘贴到其中
  3. 运行它
  4. 您可以获得有关问题所在位置的更多信息。