0

我想从存储它的表阈值中获取lower_limit值,但我总是得到:"Error RunTime 94: Invalid use of Null value"。我不希望子例程返回 lower_limit,它只对SearchAbnormalities的进一步步骤有用。我检查了表Threshold(其中 PK 由ID_parInputrangeageingender组成)并且有对应于该选择的行。

Private Sub SearchAbnormalities(ByVal ID_parInput As Integer, ByVal gender As String, ByVal eta As Integer, Optional ByVal insertedSysValue As Integer, Optional ByVal insertedDiaValue As Integer, Optional ByVal measureValue As String)

Dim rangeagein As String
Dim lower_limit As Integer
Dim upper_limit As Integer
    
rangeagein = giveMeTheRange(eta)
Debug.Print ID_parInput, gender, rangeagein '>>   12, female, middle(31-60)
    
lower_limit = DLookup("lower_limit", "Threshold", "rangeage ='" & rangeagein & "' And ID_par = " & ID_parInput & " And gender = '" & gender & "'")
    
'...

End Sub
4

1 回答 1

2

这不是处理DLookup().

当未找到查找值且常规值类型变量(字符串、整数、日期等)不能接受空值时,该DLookup()函数返回。Null尝试分配 null,将产生著名的运行时错误94 - Invalid use of Null

因此,您需要将返回值分配给 a Variant,它接受空值。然后您可以检查是否返回值并采取相应措施。

Dim returnValue As Variant, lower_limit As Integer

returnValue = DLookup("lower_limit", "Threshold", "rangeage ='" & rangeagein & "' And ID_par = " & ID_parInput & " And gender = '" & gender & "'")

'check for null
If IsNull(returnValue) Then
    'action for not found value
    'you may as well exit here
End If

'found, safe to assign
lower_limit = returnValue 

另一种方法是使用Nz()内置函数包装 DLookup,它的作用基本相同。如果未找到该值,则返回第二个参数。

Dim value As String
value = Nz(DLookup("a","b","c=0"), "Not found")

关于返回的空值,DLookup 对我来说看起来不错。事实上,打印where条件会生成:

'rangeage ='middle(31-60)' And ID_par = 12 And gender = 'female' 

由于它返回 null,因此您最好仔细检查您的数据。

于 2021-05-27T13:21:39.073 回答