Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking Optional Parameter

Status
Not open for further replies.

Hackster

Programmer
Mar 28, 2001
173
US
In a sub I have an optional parameter declared As TextBox.

How can I check to see if anything was passed to this parameter?

I've tried IsEmpty, IsNull and others and nothing has worked.

If a textbox name is passed, then I want to SetFocus to that textbox (which works when I do pass it a valid textbox name). But, if no textbox name is passed, then I get an error of "object variable or with block variable not set
 
I would use:
Code:
If <yourparameter> Is Nothing Then...
I think the best thing to do in this situation is to step through your code without passing the parameter, then you can check the value of the parameter when it's empty and you can then see what you need to check for.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
To check optional parameters, the data type needs to be variant so that you can use the IsMissing function.

I just tested this. Open a new vb project. put 1 text box and 2 command buttons on the form. Then copy/paste this code.

Code:
Option Explicit

Private Sub SetTheFocus(Optional ByRef oTextBox As Variant)
    
    If IsMissing(oTextBox) Then
        Exit Sub
    Else
        oTextBox.SetFocus
    End If
    
End Sub

Private Sub Command1_Click()
    
    Call SetTheFocus(Text1)
    
End Sub

Private Sub Command2_Click()
    
    Call SetTheFocus
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Just noticed...
Hackster said:
and nothing has worked
Hopefully...[wink]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
This worked. Thanks
Code:
Private Sub SetTheFocus(Optional ByRef oTextBox As TextBox)
    
    If IsMissing(oTextBox) Then
        Exit Sub
    Else
        oTextBox.SetFocus
    End If
    
End Sub
 
Hey George,

I just tested IsMissing with an integer variable and it works. I suspect you got the variant thing because of ParamArray having to be variant and also having to be at the end of the arg list.

Quandoque bonus dormitat Homerus......

Bob
 
Then something screwy is going on. I did this...

Code:
Option Explicit

Private Sub Form_Load()

    Debug.Print "Testing with integer"

    Call TestOptionalParameter(1)
    Call TestOptionalParameter
    
    Debug.Print "Testing with variant"
    
    Call TestOptionalParameterVariant(1)
    Call TestOptionalParameterVariant

End Sub

Private Function TestOptionalParameter(Optional ByRef TheParameter As Integer)
    
    Debug.Print IsMissing(TheParameter)
    
End Function

Private Function TestOptionalParameterVariant(Optional ByRef TheParameter As Variant)
    
    Debug.Print IsMissing(TheParameter)
    
End Function

In the debug window, I get...
[tt][blue]Testing with integer
False
False
Testing with variant
False
True[/blue][/tt]


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Just to protect myself I always explicitly set a default value on optional arguments (except for variants)
Code:
Sub SetTheFocus(Optional ByRef oTextBox As TextBox [COLOR=red]= Nothing[/color])
That way I know what value to expect if the argument was not passed.

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Straight from MSDN:

Returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.

and...

Note 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.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I never do this either.

When I want to modify the parameter list for a function/subroutine, I prefer to make the new parameter be required. The next time I compile, I'll get an error at each place the function is used. This way, I'll know that I've modified all the code that needs to be modified.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
The one problem with providing defaults is that there is a subtle difference between such a default and a missing parameter
 
strongm
Can you elucidate?

In my own coding I really don't care if a parameter was supplied or not. I simply care what value it has upon entry to the routine, regardless of how it got that value (i.e. a real argument or a default.)

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
IN which case you are ignoring the distinction (which is fine, if you don't want it use it)

But say I have a MinMax function, which may have a cap on the Maximum


eg.

Define Function CappedMinMax(a as long, b as long, optional cap as long)

The problem here is how do we indicate whether the cap is required or not UNLESS we agree that we can have it missing (setting it to a default value won't help us because we'd then have to agree that at least one value couldn't be used a a Max)

i.e the fact that an optional parameter is missing can convey important information that using a default cannot help us with
 
True enough, strongm, and an interesting point. However, isn't your example analogous to providing a default value of 0, since as we have found here, the isMissing function only works with Variants? I tested yours and if you don't provide a value for cap, it gets set to 0. Did you intend cap as a variant, or am I missing something?

 
No, actually you are exactly on my point. If I don't declare as a variant I get a default value whether I want it or realise it not ...
 
Oh! Now I get it. You're providing an example of what NOT to do. LOL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top