Solved How to check if an object has been passed to a function?

KitaYama

Well-known member
Local time
Today, 19:21
Joined
Jan 6, 2022
Messages
2,044
I have this code :

SQL:
Public Function CreateFilter(Optional frm As Access.Form) As String

    If IsMissing(frm) Then
        Set frm = Screen.ActiveForm
    End If

End Function

When I call the function without any parameter I receive an error on following lines because frm is empty.
How can I check if frm has been passed to the function or not.
IsMissing always returns false, because frm is an object and not a variant.

I've also tried IsEmpty & IsNull. They also always return False.

How can I check if a form has been passed to the function or not?
I can change the data type of frm to string and check if frm="" or not. But for now I prefer to pass a form and not its name.

Thank you.
 
There is always the idea that you would pass Me.Name to the function and inside the function would be set as

Code:
Set frm = Forms( passed-in-name )

If you did that you would be able to test the form name for being a zero-length string since the default value for a string is a ZLS.

However, there is also the idea that you could set a default value for the object, as say...

Code:
Public Function CreateFilter( Optional frm as Access.Frm = Nothing ) As String

Then you could perform an IF frm IS NOTHING test to see if a value was passed in.
 
@The_Doc_Man Thanks for your help.

As I said, for now I prefer not to go through what you suggested first.

I corrected my code according to your second solution.

As a conclusion, There's no way to check if an object data type is empty or not. Is that so?

Thanks again.
 
'empty' and 'missing' relate to variants datatypes, not objects
'nothing' relates to objects

see these links for more information

So this should work

Code:
Public Function CreateFilter(Optional frm As Variant) As String

    If IsMissing(frm) Then
        Set frm = Screen.ActiveForm
    End If

End Function
 
if not frm is nothing should work.


Code:
if not frm is nothing Then
        Set frm = Screen.ActiveForm
End If

sorry - it probably should be the other way round in your case.
if frm IS nothing then ....

the usual construct to avoid a run time error is to use the negative version.
so
Code:
if not frm is nothing then
   msgbox frm.name ' avoids generating a rte if frm IS nothing.
end if
 
Last edited:

Users who are viewing this thread

Back
Top Bottom