469,644 Members | 1,696 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Require Variable Declaration

NeoPa
32,203 Expert Mod 16PB
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
1 10561
MMcCarthy
14,534 Expert Mod 8TB
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Thomas Matthews | last post: by
134 posts views Thread by James A. Donald | last post: by
23 posts views Thread by Russ Chinoy | last post: by
14 posts views Thread by subramanian100in | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
11 posts views Thread by whirlwindkevin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.