我正在将错误存储到表中,但On错误并没有触发。在db.Execute行中,我将得到一个约束错误,但代码不会触发一个错误,而是继续下去,就好像它成功了一样。当我退出例程时,我知道有一个错误,因为0被传回了,但是我不知道错误是什么,所以我不得不带着调试,捕获代码,然后在Access中直接尝试它。不管怎样,我能让它触发错误吗?这是我的代码(缩写):
Private Function storeHAP(list of arguments, ByRef db As DAO.Database) As Long
Dim sql As String
On Error GoTo foundError
sql = "INSERT INTO HAPs (list of column names...) " + _
"VALUES (list of values...)"
db.Execute (sql)
'This function will look up the record and return the primary key.
'A value of 0 indicates it wasn't stored and will force an exit in
'the main routine.
storeHAP = lookupHAP(list of arguments..., db)
Exit Function
foundError:
MsgBox "Error in storeHAP: " + CStr(Err) + ", " + Error(Err) + ". SQL: " + sql + "."
End Function 'storeHAP发布于 2016-09-15 18:34:35
来自文献资料 (我的重点):
在Microsoft工作区中,如果提供语法正确的SQL语句并具有适当的权限,则 Execute方法不会导致失败--即使不能修改或删除一行。因此,在使用Execute方法运行更新或删除查询时,始终使用dbFailOnError选项。此选项生成运行时错误,如果任何受影响的记录被锁定,无法更新或删除,则回滚所有成功的更改。
您需要指定dbFailOnError选项:
db.Execute sql, dbFailOnError https://stackoverflow.com/questions/39517276
复制相似问题