Beaker McChemist
Beaker McChemist

Reputation: 55

In VBA, how to handle same error differently from different variables?

I have a VBA function in Access in the After_Update for a combobox that has two variables. The first one comes from the combobox and the second is a query result based on the first. I created some error handling to deal with Null values, but I would like to handle the errors differently depending on which value is Null. Sometimes users will enter a name in the combobox and then delete it, which makes the variable Null. If childID is Null that is not a problem and I would like the function to simply exit. If visitID is Null, I would like it to display the error message box. What I came up with displays the error for Null values from both variables.

On Error GoTo Error_Handle

childID = Me.cmbChildSearchFirst.Column(0)
visitid = DLookup("Visit.[VisitID]", "Q_VisitID_from_ChildID", "[ChildID] =" & childID)

Error_Handle:
If Err.Number = 94 Then
    MsgBox ("Child is not associated with any visit")
    Exit Sub
End If

Upvotes: 0

Views: 28

Answers (1)

wazz
wazz

Reputation: 5068

Basic outline:

childID = Me.cmbChildSearchFirst.Column(0)

'proceed only if not null.
If Not IsNull(childID) Then
    If Not IsNull(DLookup("Visit.[VisitID]", "Q_VisitID_from_ChildID", "[ChildID] =" & childID)) Then
        ....
    Else
        MsgBox ("Child is not associated with any visit")
        Exit Sub
    End If
End If

Upvotes: 1

Related Questions