473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Require Variable Declaration

NeoPa
32,556 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 11827
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

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

Similar topics

7
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
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
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
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
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
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
2
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.