1

我在一个表单上有一些 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 为空时,我需要向此 VBA 代码添加什么条件以自动填充零值?

4

2 回答 2

2

您可以将DLookupNz直接提供给文本框并将其分配给文本框,从而简化代码。

Private Sub cboProjectID_Change()
    Me.txttotalerrors = Nz(DLookup("[total errors]", _
        "[Project_Total_Errors_Query]", _
        "[Project_ID] = " & VarComboKey), 0)
End Sub
于 2013-07-15T19:05:37.227 回答
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
于 2013-07-15T18:31:20.637 回答