sql - 更新 SQL 错误:您输入的表达式引用了已关闭或不存在的对象
问题描述
我正在使用下面的代码来更新我的数据库中的一个字段。
Dim db As Database
Dim LSQL As String
Dim Lrs As DAO.Recordset
Set db = CurrentDb()
LUpdate = "Update tblNumber"
LUpdate = LUpdate & " set LastNbr = " & Lrs("Last_Nbr") + 1
LUpdate = LUpdate & " where Year = '" & strYear& "'"
CurrentDb.Execute LUpdate, dbFailOnError
我收到的错误:
您输入的表达式引用了一个已关闭或不存在的对象
解决方案
You haven't initialized the recordset variable Lrs
.
So using Lrs("Last_Nbr")
will throw this error.
If you want to increase the value in the table by one, simply do
Update tblNumber
Set LastNbr = Last_Nbr + 1
Where [Year] = ...
See also here:
How to debug dynamic SQL in VBA
推荐阅读
- variables - RobotFramework:变量范围
- spring - 在回滚的测试中手动执行事务中的代码
- amazon-web-services - 查看器请求和查看器响应的单个 Lambda@Edge 函数调用
- javascript - 内容丰富的 JS:无法通过匹配参考字段来检索条目
- python - 如何扩大数据框 - 熊猫
- javascript - 如何在javascript中用“”(空格)替换字符串中长度小于3的所有单词?
- javascript - svg.js 中的动画没有相同的结果
- python - 将数据框值作为参数添加到 sql 查询
- selenium - 为 Safari 运行机器人框架脚本时出现问题
- android - 如何从浏览器打开我的 React Native 应用程序?