我在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都是数字
谢谢你的帮助!
我看到两个问题:
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 & "'"
对于本文的未来读者,重新考虑将VBA值插入或连接到动态SQL查询中。考虑使用MS Access'parameterized queries的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