views:

355

answers:

3

How do I test if optional arguments are supplied or not? -- in VB6 / VBA

Function func (Optional ByRef arg As Variant = Nothing)

    If arg Is Nothing Then   <----- run-time error 424 "object required"
        MsgBox "NOT SENT"
    End If

End Function
+2  A: 

If IsMissing(arg) Then ...

Pontus Gagge
+2  A: 

Use IsIssing:

If IsMissing(arg) Then
    MsgBox "NOT SENT"
End If

However, if I remember correctly, this doesn’t work when giving a default for the argument, and in any case it makes using the default argument rather redundant.

Konrad Rudolph
Also I think IsMissing only works if the argument is declared as a variant
Jon Fournier
@Jon: true, since `IsMissing` is implemented in terms of a flag in the `VARIANT` struct (IIRC, `VT_EMPTY`). I didn’t mention this since the OP’s question already used `Variant` anyway.
Konrad Rudolph
+1  A: 

You can use the IsMissing() Function. But this one only works with the Variant datatype.

Florian