如果为null则显示零

时间:2013-07-15 18:24:25

标签: vba ms-access null zero

我在表单上有一些VBA代码,可以查询查询中的错误总数。

Private Sub cboProjectID_Change() 
   Dim VarTotalErrors As Variant 
   VarTotalErrors = DLookup("[total errors]", "[Project_Total_Errors_Query]", "[Project_ID] = " & VarComboKey) 
   Me.txttotalerrors = VarTotalErrors 
End Sub

当total_errors为null时,我需要添加到此VBA代码以自动填充零值的条件是什么?

2 个答案:

答案 0 :(得分:2)

您可以将DLookup值提供给Nz并将结果直接分配到文本框,这样可以简化代码。

Private Sub cboProjectID_Change()
    Me.txttotalerrors = Nz(DLookup("[total errors]", _
        "[Project_Total_Errors_Query]", _
        "[Project_ID] = " & VarComboKey), 0)
End Sub

答案 1 :(得分:1)

Private Sub cboProjectID_Change() 
   Dim VarTotalErrors As Variant 
   VarTotalErrors = DLookup("[total errors]", "[Project_Total_Errors_Query]", "[Project_ID] = " & VarComboKey) 
   If Nz(VarTotalErrors) = 0 Then
       Me.txttotalerrors = "0" 
   else
       Me.txttotalerrors = VarTotalErrors 
   end if
End Sub