Access 2007 Variable naming

Ziggy1

Registered User.
Local time
Today, 23:11
Joined
Feb 6, 2002
Messages
462
I always thought it was good practice to group similar variable together on a line (within reason).

eg

Dim std20, std40, HC40, HC45 As Boolean

I just discovered that the above does not work.

I have check boxes and for example if I uncheck the std40 & HC45 the variable results are as follows....

std20
Value: -1
Type: Variant/Integer

std40
Value: 0
Type: Variant/Long

HC40
Value: -1
Type: Variant/Integer

HC45
Value: FALSE
Type: Boolean


if I now change the variable declaration to ....


Dim std20 As Boolean
Dim std40 As Boolean
Dim HC40 As Boolean
Dim HC45 As Boolean

they all come across correctly as Boolean and TRUE or FALSE ???


I don't need this crap, I feel like I am still debugging beta. Is there a rule I am missing for this method of declaring variables. I usually do it the long way but sometimes I like to keep it tidy so I think I am doing a better job like this??? grrr!
 
Yes the rule you are missing is each variable must be dimmed explicitly, otherwise you get Variant by default.

You can say
Dim std20 As Boolean
Dim std40 As Boolean
Dim HC40 As Boolean
Dim HC45 As Boolean
or
Dim std20 As Boolean, std40 As Boolean,HC40 As Boolean, HC45 As Boolean

But
Dim std20, std40, HC40, HC45 As Boolean

will make std20, std40 and HC40 as variant
and HC45 as Boolean

as you have discovered. This IS NOT NEW. That's the way it's been since I started with A97. I don't know about Access 2.
 
Dim std20, std40, HC40, HC45 As Boolean

So std20, std40 and HC40 will be declared as Variant. If you think about it, all you're doing is separating the variable declarations using the comma in one line. It will be nice if there was an extra switch that was like
Code:
Dim std20, std40, HC40, HC45 As Boolean [B]With CascadeAll[/B]
... which will force all the other variables to inherit the type of the last variable. Unfortunately, there's no such switch.

Dim std20, std40, HC40, HC45 As Boolean is the same as
Dim std20 As Variant, std40 As Variant, HC40 As Variant, HC45 As Boolean

So I'm sure you can now see how to declare all of them in one line with their appropriate types.

Edit: a bit late there. looks like jdraw beat me to it :)
 
thanks guys, I am really surprised. I have seen people do it on one line and that is the only reason I ever did it. luckily I don't do it all the time, so I will have to review my code.
 
It works in the way we explained. Re-read our posts ;)
 
ok gotcha..

Dim std20 As Boolean, std40 As Boolean,HC40 As Boolean, HC45 As Boolean
 

Users who are viewing this thread

Back
Top Bottom