473,513 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Require Variable Declaration

NeoPa
32,557 Recognized Expert Moderator MVP
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 11849
MMcCarthy
14,534 Recognized Expert Moderator MVP
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

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

Similar topics

7
2646
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
2
8804
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
7750
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
23
19134
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
4
2684
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
14
13026
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
4691
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
112
5351
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
2900
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.