2013-07-15 98 views
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代碼中以自動填充零值的條件是什麼?

回答

2

您可以DLookup值喂Nz和結果的文本框中直接,從而簡化代碼分配。

Private Sub cboProjectID_Change() 
    Me.txttotalerrors = Nz(DLookup("[total errors]", _ 
     "[Project_Total_Errors_Query]", _ 
     "[Project_ID] = " & VarComboKey), 0) 
End Sub 
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