【发布时间】:2016-05-25 13:43:26
【问题描述】:
我目前正在编写 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
请帮忙
【问题讨论】:
-
可能是数据输入错误,为什么对
Me.TxtId和Me.TxtId.Tag的引用没有用单引号封装?这些字段不会像文本框通常那样返回字符串吗? -
括号字段名称包含除字母、数字和下划线 (
_) 以外的任何字符:[Cellphone/Telephone]和[Address (Fisical)]换句话说,将这些字段名称用于UPDATE你是为INSERT做的。 -
考虑切换到参数查询,而不是将值连接到 SQL 语句文本中。
-
我确实尝试过封装 me.txtID 和 me.txtId.Tag 但在编译时它给了我一个错误,类似于预期的语句,已经将字段括起来,但它的作用相同,语法错误,并且我无法将值更改为 SQL 语句文本:S.. 很困惑,我知道 SQL,但我对它没有那么有经验
标签: vba command ms-access-2013