我在此代码CurrentDb.Execute中有一个运行时错误3061

时间:2018-01-07 12:37:07

标签: sql vba ms-access updates

我在此代码中遇到运行时错误3061

CurrentDb.Execute "UPDATE [tblCategory]" & _
 " SET [Category]=" & Me.txtCat & _
 ", [Percentage]='" & Me.txtPer & "'" & _
 " WHERE [Category]=" & Me.txtCat.Tag

1 个答案:

答案 0 :(得分:1)

考虑参数化查询以避免需要字符串连接和引用机箱,因为您在PARAMETERS子句中明确定义了类型,所有这些都是可读和可维护的例程。下面假设百分比是十进制数(即双精度数)。

SQL (另存为存储的查询)

PARAMETERS [CategParam] Text, [PctParam] Double, [CategTagParam] Text;
UPDATE [tblCategory]
SET [Category] = [CategParam], [Percentage] = [PctParam]
WHERE [Category]= [CategTagParam]

<强> VBA

Dim qdef As QueryDef

Set qdef = CurrentDb.QueryDefs("mySavedQuery")

qdef![CategParam] = Me.txtCat
qdef![PctParam] = Me.txtPer
qdef![CategTagParam] = Me.txtCat.Tag

qdef.Execute dbFailOnError

Set qdef = Nothing