vba - VBA访问无效使用Null
问题描述
今天刚开始使用 Access VBA,想象这是一个简单的修复。该程序计算每个服务类别的客人总数。我一定错过了一些简单的东西。
Public Sub CalculateTotalGuestsForEachService()
'Declare variables
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim intTotalParty As Integer
'Set the current database
Set db = CurrentDb
'Set the recordset
Set rst = db.OpenRecordset("Select Orders.* From Orders Where ServiceID = 1")
'Cycle through the records
Do While Not rst.EOF
intTotalParty = intTotalParty + rst!NoInParty
rst.MoveNext
Loop
'Display total amount
MsgBox "The total is " & intTotalParty
'Close the recordset
rst.Close
Set rst = Nothing
Set db = Nothing
End Sub
解决方案
如果任何记录具有Null值,则应用Nz:
intTotalParty = intTotalParty + Nz(rst!NoInParty.Value, 0)
或者,您可以让查询对这些值求和:
'Set the recordset
Set rst = db.OpenRecordset("Select Sum(NoInParty) As TotalParty From Orders Where ServiceID = 1")
If rst.RecordCount = 1 Then
intTotalParty = Nz(rst!TotalParty.Value)
End If
推荐阅读
- python - 如何从我的有序字典创建熊猫数据框?
- python - 如何检查文本特征的特征重要性?
- c# - 手动加载后,Planner 才会出现
- iis - IIS 8 Url重写规则否定不起作用
- mysql - 如何编写返回具有最多事务计数的项目名称的查询?
- docker - Dockerfile CMD 没有通过引号传递给 /bin/sh -c 入口点
- python - 从带有分类的熊猫非均匀列中提取多个数字
- dsl - 用于复杂 JSON 对象的 karate.filterKeys() API
- c - 降级后 GCC 无法编译简单的 C 应用程序
- html - 使用R markdown,有没有办法修复可滚动(html)、kable 表的行名,类似于scroll_box 函数修复标题行?