Jolio81
asked on
Check for input in optional form variable
I have a function with some arguments, and one of them is optional and of the "form" variable type.
Public Function MyFunction(Argument1 as Boolean, Optional TheForm as Form) as String
If TheForm is null Then
'Do something
End If
End Function
"TheForm is null" always returns false, whether or not anything has been entered for that argument.
Is there an explicit way the to test whether something was entered for TheForm argument, without using error trapping, or using a variant variable type? That should be possible, right??
Public Function MyFunction(Argument1 as Boolean, Optional TheForm as Form) as String
If TheForm is null Then
'Do something
End If
End Function
"TheForm is null" always returns false, whether or not anything has been entered for that argument.
Is there an explicit way the to test whether something was entered for TheForm argument, without using error trapping, or using a variant variable type? That should be possible, right??
Try using IsEmpty() when referring to an object.
ASKER
Public Function MyFunction(Argument1 As Boolean, Optional TheForm As Form) As String
MyFunction = "Is TheForm Empty? " & IsEmpty(TheForm)
End Function
In the Immediate window, I type:
debug.print myfunction(True)
The result I get is this:
Is TheForm Empty? False
MyFunction = "Is TheForm Empty? " & IsEmpty(TheForm)
End Function
In the Immediate window, I type:
debug.print myfunction(True)
The result I get is this:
Is TheForm Empty? False
Try IsMissing() or Is Nothing
Public Function MyFunction(Arguement1 as boolean, Optional TheForm as Form) as String
MyFunction = "Is TheForm Missing: " & isMissing(TheForm) & vbcrlf _
& "Is TheForm Nothing: " & (TheForm is nothing)
End function
I think you will find, that the Is Nothing syntax is what you are looking for.
Public Function MyFunction(Arguement1 as boolean, Optional TheForm as Form) as String
MyFunction = "Is TheForm Missing: " & isMissing(TheForm) & vbcrlf _
& "Is TheForm Nothing: " & (TheForm is nothing)
End function
I think you will find, that the Is Nothing syntax is what you are looking for.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fyed,
I tried this:
Public Function MyFunction(Argument1 As Boolean, Optional TheForm As Form) As String
MyFunction = "Is TheForm nothing? " & TheForm Is Nothing
End Function
but it threw "Compile error: Type mismatch". I also tried "TheForm = Nothing" but got the same.
IsMissing didn't work either; the function returned "Is TheForm missing? false"
I tried this:
Public Function MyFunction(Argument1 As Boolean, Optional TheForm As Form) As String
MyFunction = "Is TheForm nothing? " & TheForm Is Nothing
End Function
but it threw "Compile error: Type mismatch". I also tried "TheForm = Nothing" but got the same.
IsMissing didn't work either; the function returned "Is TheForm missing? false"
ASKER
Works great! Thanks hnasr!
Well, if you had included the parenthesis around the expression, as I did in my example, it would have worked properly.
MyFunction = "Is TheForm Nothing: " & (TheForm is nothing)
When you left out the parenthesis, Access assumed you wanted to contatenate the TheForm (which "is nothing" as it was not passed to the function) to the text "Is TheForm Nothing", which is why you got a type missmatch. By including the parenthesis, you force evaluation of the Is Nothing test and then concatenate either True or False to the string.
MyFunction = "Is TheForm Nothing: " & (TheForm is nothing)
When you left out the parenthesis, Access assumed you wanted to contatenate the TheForm (which "is nothing" as it was not passed to the function) to the text "Is TheForm Nothing", which is why you got a type missmatch. By including the parenthesis, you force evaluation of the Is Nothing test and then concatenate either True or False to the string.
Welcome!
ASKER
Dear Fyed,
Let us blame those at Microsoft for the willy-nilly behavior of the compiler. The following does work properly without parentheses:
debug.Print "Does not true = false? " & not true = false
and yet
"Is TheForm nothing? " & TheForm Is Nothing
does not. I think that's dumb. Sorry, I would have definitely given you points.
Let us blame those at Microsoft for the willy-nilly behavior of the compiler. The following does work properly without parentheses:
debug.Print "Does not true = false? " & not true = false
and yet
"Is TheForm nothing? " & TheForm Is Nothing
does not. I think that's dumb. Sorry, I would have definitely given you points.
@jolio81
you have to set some form of precedence when doing evaluation, what's the old adage
Parentheses, exponentiation, multiplication, division, addition, subtraction
in the case above, I'm sure the "=" and "Not" operators have priority over the "&"
doesn't appear that "xxxxx is Nothing" is higher in priority than "&", which is why I generally explicitly wrap this type of thing in parentheses.
No prob on the points, the point in my explanation was to help you understand the behavior.
you have to set some form of precedence when doing evaluation, what's the old adage
Parentheses, exponentiation, multiplication, division, addition, subtraction
in the case above, I'm sure the "=" and "Not" operators have priority over the "&"
doesn't appear that "xxxxx is Nothing" is higher in priority than "&", which is why I generally explicitly wrap this type of thing in parentheses.
No prob on the points, the point in my explanation was to help you understand the behavior.
ASKER
I appreciate the advice; I'll start making a habit of using parentheses. Thanks Fyed!