Solved How to check if an object has been passed to a function? (1 Viewer)

KitaYama

Well-known member
Local time
Today, 22:15
Joined
Jan 6, 2022
Messages
1,541
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:15
Joined
Feb 28, 2001
Messages
27,194
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.
 

KitaYama

Well-known member
Local time
Today, 22:15
Joined
Jan 6, 2022
Messages
1,541
@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.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:15
Joined
Feb 19, 2013
Messages
16,619
'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
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:15
Joined
Sep 12, 2006
Messages
15,658
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

Top Bottom