来自UPDATE语句的运行时错误3144

问题描述 投票:-1回答:2
CurrentDb.Execute "UPDATE Transaction SET receipt_id=" & txtreceipt_id & ", tdate=" & txttdate & ",total_cost=" & txttotal_cost & ",total_disc=" & txttotal_disc & " WHERE receipt_id=" & txtreceipt_id & " "

我得到运行时3144语法错误。似乎找不到错误。

sql access-vba ms-access-2010 ms-access-2013
2个回答
0
投票

考虑使用带有MS Access'parameterized queryQueryDefs来准确指定绑定值的数据类型,并避免连接和引用包装,这使得难以维护代码。根据需要在PARAMETERS子句中调整以下类型(在Access SQL中兼容)。

SQL(仅保存为MS Access存储查询一次)

PARAMETERS [txtreceipt_id_PARAM] LONG, [txttdate_PARAM] DATE,
           [txttotal_cost_PARAM] DOUBLE, [txttotal_disc_PARAM] DOUBLE;
UPDATE [Transaction] 
SET receipt_id = [txtreceipt_id_PARAM], 
    tdate = [txttdate_PARAM],
    total_cost = [txttotal_cost_PARAM], 
    total_disc = [txttotal_disc_PARAM]
WHERE receipt_id = [txtreceipt_id_PARAM];

VBA(将值动态绑定到参数占位符)

Dim qdef as QueryDef

Set qdef = CurrentDb.QueryDefs("mySavedQuery")

qdef![txtreceipt_id_PARAM] = txtreceipt_id
qdef![txttdate_PARAM] = txttdate
qdef![txttotal_cost_PARAM] = txttotal_cost
qdef![txttotal_disc_PARAM] = txttotal_disc

qdef.Execute dbFailOnError

Set qdef = Nothing

0
投票

所以看起来语法稍微有点......它可能有助于将代码分成几部分......我无法测试这个解决方案但是尝试这个(这一切都取决于数据类型):

CurrentDb.Execute "UPDATE Transaction " & _
" SET receipt_id = '" & txtreceipt_id & "'" & _
", tdate = "  & txttdate & "" & _
", total_cost = " & txttotal_cost & "" & _
", total_disc = " & txttotal_disc & "" & _
" WHERE receipt_id = '" & txtreceipt_id & "'"

如果txreceipt_id是一个数字,那么尝试:

CurrentDb.Execute "UPDATE Transaction " & _
" SET receipt_id = " & txtreceipt_id & "" & _
", tdate = " & txttdate & "" & _
", total_cost = " & txttotal_cost & "" & _
", total_disc = " & txttotal_disc & "" & _
" WHERE receipt_id = " & txtreceipt_id & ""
© www.soinside.com 2019 - 2024. All rights reserved.