How do I put an option explicit statement in my access database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Access suggests that I use option explicit statements for each form in my
mdb. I am not terribly clear on the benifits and what code to write for each
form?? I can't seem to find any decent documentation or help on this
topic..Any help would be apprieciated..Thanks
 
hi,
Access suggests that I use option explicit statements for each form in my
mdb. I am not terribly clear on the benifits
When using Option Explicit, VBA checks that every use variable name is
declared before use.

The main reason for using it, is to reveal typos. When you have
misspelled a variable name in code and you do not use Option Explcit,
the misspelled variable will be treated as new variable.

Dim i As Long

' Assign 123 to i
j = 123

Debug.Print i

When using Option Explicit and the Debug / Compile menu item in the VBA
IDE it will say that j is undeclared.
[..] and what code to write for each form??
So normally every module starts with

Option Compare Database

Just append the

Option Explicit

below this line.



mfG
--> stefan <--
 
More than just for each form ...

For any module your database has, including form modules, report modules,
code modules (and class modules), you want the expression:

Option Explicit

as the first line in the module.

If you have an already-existing Access .mdb database, you will need to open
each module and put that expression at the top.

If you use a "template" Access .mdb file to set up a new database, open a
module, change the option (Tools | Options ...) that governs variable
declaration (Require Variable Declaration). This tells the VBA editor that
any module must start with "Option Explicit", which means you cannot add
code that refers to a variable that has NOT been "Dim'd".

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Open the VBA editor, click Tools > Options, click the Editor tab, and select
Require Variable Declaration to have the Option Explicit statement
automatically added to new code modules. I believe the setting that will
apply to future databases (i.e. you only need to make that selection once).
 
Back
Top