sql-server - Microsoft Access 到 SQL Server 转换 - 插入时无效使用 null
问题描述
我正在迁移旧版 Microsoft Access 应用程序以使用 SQL Server 作为后端。我注意到自从切换到 SQL Server 以来,所有插入现在都出现了无效使用 null 错误。
通过一些测试,我发现这是由于 VBA 是如何编写的。主键在创建记录之前被引用,毫不奇怪它为空。
Dim recordSet As DAO.Recordset
Dim newID As Long
Set recordSet = dbLocal.OpenRecordset("Select * FROM tblUser", dbOpenDynaset, dbSeeChanges)
With recordSet
.AddNew
newID = !UserID
.Update
End With
recordset.Close
现在这在旧的 Access to Access 应用程序中工作正常。它是如何做到这一点的,我怎样才能使它与新的 SQL Server 后端一起工作?(我知道我可以将代码更改为不引用它,但是这会在应用程序中多次发生,因此我不希望这样做)。
解决方案
我很惊讶这曾经奏效。正确的做法是这样的:
With myRecordSet
.AddNew
' Set all data columns (not the IDENTITY column)
!Data = someData
' Save new record
.Update
' Move the recordset pointer to the new record to read its ID
.Bookmark = .LastModified
newID = !UserID
' Done
.Close
End With
推荐阅读
- python - 如何在两个不同的 Github 存储库中同步一些文件夹?
- oop - 实现相同接口的不同类进入构造函数
- python - 具有重叠部分的图像的字符分割
- python - 如何从两个分组的列中找到数据集中的最大值
- pycharm - Pycharm调试器面板的个性化
- c++ - 为什么 noreturn/__builtin_unreachable 会阻止尾调用优化
- c# - 未注册“ProjectModels.Graphs.ProjectGraph”类型的服务
- r - R - 在地图中绘制不同颜色区域的轮廓
- vue.js - 使用 VueX 时,将整个数据项与仅 id 作为 Vue 列表中的 Prop 传递
- python-3.x - 仅每 5 秒触发一次(聊天触发的机器人)的问题