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
- Public Sub HWorld()
- Var = "Hello World"
- MsgBox Var
- End Sub
How to Set Variable Declaration :
- Go to the VBA Window (Alt-F11 from Access).
- Open the Options page (Tools / Options).
- Select Require Variable Declaration from the Editor tab.