tags:

views:

579

answers:

4

I'm making some significant changes to some VBA code, and some variables are being deleted and/or renamed. It would be a lot easier to find all the locations I need to update if the compiler would complain to me that the variables don't exist instead of creating it on the fly.

+8  A: 
Option Explicit
Adam Bernier
+1  A: 

This question was quite helpful for Excel, you might see if it'll work for Access:

Lost Variables

Lance Roberts
+14  A: 

You need to use Option Explicit at the top of each VBA code module including forms and reports.

You can set this for all future created modules and VBA code behind forms and reports by going into the VBA editor >> Tools >> Options >> Editor tab and ensuring Require Variable Declaration is checked.

From Access 2003 help

Require Variable Declaration — Determines whether explicit variable declarations are required in modules. Selecting this adds the Option Explicit statement to general declarations in any new module.

I also use camel case when I Dim my variables. ThisIsAnExampleOfCamelCase. As soon as I exit the VBA code line if Access doesn't change the lower case variable to camel case then I know I've got a typo.

Tony Toews
+10  A: 

Some History on OPTION EXPLICIT and Access VBA

To follow on from Tony's answer, here's some explanation of why there are issues with OPTION EXPLICIT not being on in some Access code modules.

In Access 95 and Access 97 (the first two Office versions with VBA), Access had a different code editor than the other office programs. In Access 2000, Microsoft implemented the VBE from the other Office apps in Access. At the same time, MS chose to make Access VBA modules behave like the modules in the other apps, which defaulted to not having OPTION EXPLICIT.

Thus, in Access 2000, by default, modules were created without OPTION EXPLICIT.

This was, of course, a really stupid design decision on MS's part, and they reversed it later (I can't remember if it was Access 2002 or 2003 that rectified the problem and defaulted to OPTION EXPLICIT in all new modules again). The reason it was dumb (and MS should have known this) is because Access is a DATABASE APPLICATION DEVELOPMENT tool, and thus is operating on data that is strongly typed. Thus, the code environment should be strongly typed by default so that it is in harmony with the data it is working with.

In Excel or Word, the data is not strongly typed, and it thus makes more sense to use variant data types for just about everything, simply to make it easier for everyone. The downside of implementing that by not using OPTION EXPLICIT is that you can end up with typos that automatically intrdoduce new variables [such as the variable "intrdoduce" -- if I was writing this post with OPTION EXPLICIT, that wouldn't have happened without producing a runtime error! :)]. This is a problem with all such languages that work this way (I pull my hair out working in PHP, where variable names can be distinct by case, i.e., $Var is not the same variable as $var; but I digress), but MS made the decision to implement it that way in Word and Excel on the theory that the people writing code there are going to have an easier time of it if they aren't forced to declare their variables.

So, MS made the mistake of making Access's version of the VBE like the other apps, even though there was no logic internal to Access's own purposes that supported that move. And MS then backed out that change and returned to the previous status quo (i.e., OPTION EXPLICIT by default in all modules).

Thus, you will often see apps that began life in Access 2000 that have modules all over the place without OPTION EXPLICIT. When I have to work on such an app, my first task is to implement OPTION EXPLICIT in all modules and then fix the damned thing so it will compile (which is often quite tough, given that the thing was programmed without it).

David-W-Fenton
Excellent follow-up David!!! +1 I'd give you more but the S.O. creators have wisely prevented me from doing so :)
CodeSlave
Very nice history lesson. Thanks
Tony Toews
"In Excel or Word, the data is not strongly typed" -- well *my* data is strongly type (hint: some of it comes from SQL databases). In VBA I avoid the Variant data type for good reasons too numerous for a comment!
onedaywhen
One should not use the Variant data type except for variant data. The chief use of Variant data type in my apps is to be able to deal with Nulls, as you can store a Null in a variant. For instance, a UDF that's going to be used on a column in a query where there are Nulls will have its parameters defined as Variant so that the UDF doesn't croak on the Nulls. Also, variants are subtyped, so it's not as bad as all that, though you have no control over the subtyping operation.
David-W-Fenton
If your Excel data is coming from a database, it's not Word or Excel data, is it?
David-W-Fenton