We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Obnoxious Access VBA variable declaration problem

JP_TechGroup
JP_TechGroup asked
on
Medium Priority
395 Views
Last Modified: 2012-08-13
I am passing a variable between subs with an Optional switch set such that
example(data as string, optional err_code as single) is what is expected.

The receiving sub runs a series of proceedures depending on whether the err_code variable is populated. The problem I am having is referring to the err_code variable when it is empty. No iteration of null works and "" empty set also bombs out. I don't want to have to go back and populate all references to this sub with a 0, just so access will have a number to refer to when the field is supposed to be empty, so how do I achieve this? Thanks.
Comment
Watch Question

Retired IT Professional
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Hamed NasrRetired IT Professional
CERTIFIED EXPERT

Commented:
" don't want to have to go back and populate all references to this sub with a 0"

What do you mean?

Attach a sample database. Explain the issue.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
Pass the Optional variable as a Variant:

example(data as string, optional err_code as Variant)

Then use the IsMissing method to check

If IsMissing (err_code)  Then
   ' do whatever
Else
   ' do whatever else
   ' You can convert the err_code back to a Long here use CLng(err_code) if necessary
End If

Note that IsMissing ONLY works with Optional Variants

mx
I think hnasr is correct, empty will be 0, but if you want you can change your code to read "Optional err_code as Single = 0" which will set the variable as 0 unless the calling procedure sets it as something else.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
And variable Dimmed (in VBA) as Numeric (Long, Integer, Single etc) will default to zero (0).  Empty does not apply to Numeric Variables.  Empty applies to a String variable ("").  Numeric variables can be set to Null, String variables cannot.

mx

Author

Commented:
And this is why I shouldn't program for 12 hours at a pop! Thanks for the head smack.
Hamed NasrRetired IT Professional
CERTIFIED EXPERT

Commented:
Welcome!
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
"then empty will be 0."
Empty does not apply to Numeric variables.

mx
Agreed. Should read 'un-assigned = 0"
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.