捕获DSum Null会导致计算错误

时间:2013-01-15 09:43:50

标签: vba ms-access access-vba

jd都会评估相同的函数,但是当我使用If IsNull来捕获任何Null值时,d的值未正确计算。造成这种情况的原因是什么?

    Dim d As Integer
    Dim j As Integer

    j = DSum("Count", "qry_nihr_unique")
    If IsNull(d = DSum("Count", "qry_nihr_unique")) Then
        MsgBox "No records were found for the data criteria you entered"
        GoTo ESub
    Else
        Me.un_p.Value = d
    End If

    Debug.Print "j = " & j
    Debug.Print "d = " & d


    j = 58
    d = 0

答案后更新的代码

    Dim d
    d = DSum("Count", "qry_nihr_unique")
    If IsNull(d) Then
        MsgBox "No records were found for the data criteria you entered"
        GoTo ESub
    Else
        Me.un_p.Value = d
    End If

在HansUp回答下面之后,我相信这是最有效的方式。

1 个答案:

答案 0 :(得分:1)

IsNull()内,代码会检查d是否等于DSum()表达式。这是一个相等测试,没有任何内容分配给d。所以d的值保持不变---它被初始化为零并保持为零。

情况非常像立即窗口会话:

? DSum("id", "tblFoo")
 134 
d = 0
? (d = DSum("id", "tblFoo"))
False
? d
 0
? IsNull(d = DSum("id", "tblFoo"))
False
? d
 0

以下语句会将DSum()结果分配给d;不测试两者是否相等:

d = DSum("id", "tblFoo")
? d
 134