Solved

Check for input in optional form variable

Posted on 2014-03-05
11
266 Views
Last Modified: 2014-03-05
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??
0
Comment
Question by:Jolio81
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39906869
Try using IsEmpty() when referring to an object.
0
 

Author Comment

by:Jolio81
ID: 39906921
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
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39906959
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.
0
 
LVL 30

Accepted Solution

by:
hnasr earned 450 total points
ID: 39907099
Try:
Public Function MyFunction(Argument1 As Boolean, Optional TheForm As Form) As String
    If TypeName(TheForm) = "Nothing" Then
        'Do something
        MsgBox ("Optioanal argument ignored")
    End If
End Function

Open in new window

0
 

Author Comment

by:Jolio81
ID: 39907377
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"
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Closing Comment

by:Jolio81
ID: 39907393
Works great!  Thanks hnasr!
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39907410
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.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39907420
Welcome!
0
 

Author Comment

by:Jolio81
ID: 39907543
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39907726
@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.
0
 

Author Comment

by:Jolio81
ID: 39907810
I appreciate the advice; I'll start making a habit of using parentheses.  Thanks Fyed!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now