Option Explicit Statement

K

Ken Snell \(MVP\)

It allows the compiler to detect misspelled / wrong variable names
(variables that you didn't Dim). Consider it an early warning system for
problems in your code.
 
A

Allen Browne

Jama, Ken and Jeff have given you good responses, but understated how
important this is. If you write VBA code, Option Explicit will save your
sanity!

If you do not use Option Explicit, and you type a name that Access does not
recognise, it must makes up a new variable with that name. You know nothing
about this, so you merrily code on, assuming that you are processing a
variable, a control or something, whereas VBA is processing something
completely different.

When you have finished writing your routine, you might not notice that
something is wrong. The program just never works the way you planned, and
noone realises. Believe me: I have seen cases like that in examining
databases, some that ran like this for over a decade!

If you did notice the results were incorrect, how will you find where it
went wrong? You have dozens of lines--possibly hundreds or even
thousands--and you are going to have to print them all out and carefully
read every line to try to find whether you mis-spelled a variable, or
whether there is a problem with the algorithm, or ???

*Anything* that can help catch a misspelling before the program goes awry
will save you days of debugging.

I presume you did read the link Jeff gave you, where Tom Wickerath quotes
Microsoft, but it's worth including for anyone else who is reading this
thread:

"However, every experienced developer knows that the failure to use the
Option Explicit statement to force explicit variable declaration is a coding
blunder of the highest order. Using undeclared variables can introduce
subtle, hard-to-find bugs into your code that are easily avoided by using
this one, simple technique."
 
A

Albert D.Kallal

Jeff Conrad said:
Hi Allen,

So now the $64,000 question:

Why does Microsoft not make this the default option?
Humm.......

That happened around a2000. My bets are one of two things:

1) it simply made the conversion routines from "older" versions of
ms-access more forgiving

2) it simply allowed people to write code without having to define the
variables...and that was easier for newbie's....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top