423,818 Members | 2,250 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Require Variable Declaration

NeoPa
Expert Mod 15k+
P: 31,112
Problem Description :
In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type Variant.

Why This Can be a Problem :
Whenever there are problems with code, having this option missing means there are more possible problems to find by yourself. Misspelled variable names are a common cause of problems which disappear almost entirely when this option is set. Another frequent problem when running in this mode, due to the very flexible and loosely defined nature of the language, is that you can write code intending to use a built-in feature but, because of a misspelling, it doesn't recognise the code as the intended feature so treats it simply as a previously undeclared variable instead. There are many and varied situations where this problem can bite you unexpectedly.

As with any problems posted on this site, you won't make many friends if you post code without compiling and testing it first. This goes for Variable Declaration too. If people offer time to help with your problem only to find that you're asking about something the compiler is perfectly capable of catching, they're likely to feel insulted and ill-used.

Why is it Possible to Run With This Unset :
Visual Basic for Applications was developed with non-developers definitely in mind. One of the driving forces was to provide a language which was very easy to pick up and get something running in. More professional languages have always (certainly since the 1970s) insisted on variable definition and declaration. At its most basic, VBA can produce a "Hello World" procedure (using a variable) simply as :
Expand|Select|Wrap|Line Numbers
  1. Public Sub HWorld()
  2.   Var = "Hello World"
  3.   MsgBox Var
  4. End Sub
Variable definitions can be left to the next chapter of the tutorial to be introduced.

How to Set Variable Declaration :
  1. Go to the VBA Window (Alt-F11 from Access).
  2. Open the Options page (Tools / Options).
  3. Select Require Variable Declaration from the Editor tab.
When this is set all newly created modules will have the line "Option Explicit" included automatically. If you ever see a module without this line, treat it with mistrust!
May 8 '08 #1
Share this Article
Share on Google+
1 Comment


MMcCarthy
Expert Mod 10K+
P: 14,534
You wouldn't believe the state of some of the "other people's code" I've been forced to correct when the Option Explicit was missing. How any of those applications functioned in any way at all I don't know.

Even after all these years (no jokes please) I still make sure Option Explicit is set before I start developing. I would never find some of the errors in my own code otherwise.
May 28 '08 #2