我在update语句中遇到语法错误。运行时错误:3144 我使用以下代码
CurrentDb.Execute "UPDATE product " & _
" SET [product name] = '" & Me.txtName & "'" & _
", [cost of product] = " & Me.txtCost & "" & _
", [weight] = " & Me.txtWeight & "" & _
", [group] = '" & Me.CmbGroup & "'" & _
", [group ID] = '" & Me.txtGroupID & "'" & _
", [Ordered] = " & Me.txtOrdered & "" & _
" WHERE [roduct name] = " & Me.txtName.Tag & ""
可能是什么问题? 如果有意义,那么Me.txtCost,Me.txtWeight和me.txtOrdered都是数字
感谢您的帮助!
答案 0 :(得分:2)
我看到两个问题:
WHERE [roduct name]
中的错字(应为WHERE [product name]
)Me.txtName.Tag
处缺少引号请改为尝试:
CurrentDb.Execute "UPDATE product " & _
" SET [product name] = '" & Me.txtName & "'" & _
", [cost of product] = " & Me.txtCost & "" & _
", [weight] = " & Me.txtWeight & "" & _
", [group] = '" & Me.CmbGroup & "'" & _
", [group ID] = '" & Me.txtGroupID & "'" & _
", [Ordered] = " & Me.txtOrdered & "" & _
" WHERE [product name] = '" & Me.txtName.Tag & "'"
答案 1 :(得分:0)
对于本文的未来读者,重新考虑将VBA值插值或连接到动态SQL查询中。考虑parameterized queries使用MS Access' QueryDefs。这样可以避免错误输入,错误引用,不可读和不可维护的代码。
SQL (仅保存为MS Access存储查询一次)
PARAMETERS [txtName_PARAM] TEXT, [txtCost_PARAM] DOUBLE,
[txtWeight_PARAM] DOUBLE, [CmbGroup_PARAM] TEXT,
[txtGroupID_PARAM] TEXT, [txtOrdered_PARAM] LONG,
[txtName_Tag_PARAM] TEXT;
UPDATE product
SET [product name] = [txtName_PARAM],
[cost of product] = [txtCost_PARAM],
[weight] = [txtWeight_PARAM],
[group] = [CmbGroup_PARAM],
[group ID] = [txtGroupID_PARAM],
[Ordered] = [txtOrdered_PARAM],
WHERE [product name] = [txtName_Tag_PARAM];
VBA (将值动态绑定到参数占位符)
Dim qdef as QueryDef
Set qdef = CurrentDb.QueryDefs("mySavedQuery")
qdef![txtName_PARAM] = Me.txtName
qdef![txtCost_PARAM] = Me.txtCost
qdef![txtWeight_PARAM] = Me.txtWeight
qdef![CmbGroup_PARAM] = Me.CmbGroup
qdef![txtGroupID_PARAM] = Me.txtGroupID
qdef![txtOrdered_PARAM] = Me.txtOrdered
qdef![txtName_Tag_PARAM] = Me.txtName.Tag
qdef.Execute dbFailOnError
Set qdef = Nothing