Pages

Sunday, April 08, 2007

Optional Parameter Example

Function Optional_Param_Example(Optional ByVal vOptionalText1 As Variant, Optional ByVal sOptionalText2 As String)

' ---------------------------------------------------------------
' Written By Shanmuga Sundara Raman for http://vbadud.blogspot.com
' ---------------------------------------------------------------

If IsMissing(vOptionalText1) Then
MsgBox "Optional Parameter 1 Missing.."
Else
MsgBox "Optional Parameter 1 Present.."
End If

IsMissing does not work on simple data types (such as Integer or Double) because, unlike Variants, they don't have a provision for a "missing" flag bit.
sOptionalText2 is not reported missing if it is not supplied

If IsMissing(sOptionalText2) Then
MsgBox "Optional Parameter 2 Missing.."
Else
MsgBox "Optional Parameter 2 Present.."
End If

' IsMissing Function, Optional Parameters, IsMissing Doesn't Work

End Function

1 comment:

  1. Anonymous11:37 AM

    I was going crazy with the use of Ismissing with string type parameters. It really only works with variant types.
    Thank you for your explanation. I have saved so much time.
    Greetings from Spain...

    ReplyDelete