UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Option Explicit    
Option Explicit

Image:NotifCleanup.gif This page requires general cleanup in formatting or text to better fit the UA Wiki Guidelines

Option Explicit is a compiler directive. Using Option Explicit enables the compiler to catch misspellings so users do not have as many surprises at run-time.

It is always a good idea to DIMension your variables before you use them -- and best to be a specific data type if it applies and you do not need to store a null (as only a variant can do)

Dim nPartID As Long

Why is this important?

Without the Option Explicit directive you could make a typo in your code (eg nPareID = 1) and VBA would assume you have wish to use a new variable even though it doesn't have an explicit DIM statement. It can take a long time to spot such a typo and much loss of hair in trying to work out why the variable nPartID does not hold the value you might expect!

To force variable declaration (which is HIGHLY recommended), put this at the top of your module:

Option Explicit

To have this included by default in all new modules, from the VBE window, you can do --> Tools, Options... and check "Require Variable Declaration" (highlighted in red in the following image) so that any new modules created will have the Option Explicit statement automatically at the top. Modules that have already been created will not be changed.


(Top tip: Uncheck 'Auto Syntax Check' (highlighted in orange) to prevent a MsgBox popping up each time you make an error. Errors in your code will simply be highlighted in red.

Another handy thing in declaring variables is that, if Access recognizes the variable name you have typed as a variable that has been DIMensioned, it will change the case for you after you move off the line -- so it is good to put one or more uppercase characters in variable names and type everything in lower case

npartid --> will be corrected to --> nPartID
as soon as you move off the line. Nice because you have an indicator that you typed the name correctly! And if you didn't, the compiler will catch it since all variables must be declared when using Option Explicit.

If you declare a variable but do not specify its type, Access will assume it is a variant -- this takes extra space. Declared variable access is slightly faster than undeclared variable access.

When you use a Variant type, which can hold any type of data -- numbers, strings, arrays, etc – you have flexibility to be lazy, and that also degrades performance. In order to work with a Variant, Access has to figure out what type of data it has, keep track of that in another place, as well as perform conversions, whereas if you specify a data type, Visual Basic can compile the code more efficiently.

Another reason to specifically declare the data type is so that the AutoComplete feature of Visual Basic can list only things that apply specifically to that data type.

other compiler directives:

Option Compare Database
This uses the default database comparison (in most locals this is the same as using Option Compare Text)
Option Compare Text
this means that "ABC" = "abc" so case is ignored when string comparisons are made
Option Compare Binary
Comparisons are made based on the binary representation of a character, therefore "ABC" does NOT equal "abc"

Option Base 1

This statement sets the default lower limit index of an array to 1 instead of the default of 0 (zero).

There are more compiler directives, but these are the most common.


this thread has some good reference information:

Code Behind Form (CBF)

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 20,437 times.  This page was last modified 11:29, 20 December 2014 by Cheekybuddha. Contributions by Jack Leach, Walter Niesz and strive4peace2010  Disclaimers