How do I force VBA/Access to require variables to be defined?

You need to use Option Explicit at the top of each VBA code module including forms and reports.

You can set this for all future created modules and VBA code behind forms and reports by going into the VBA editor >> Tools >> Options >> Editor tab and ensuring Require Variable Declaration is checked.

From Access 2003 help:

Require Variable Declaration — Determines whether explicit variable declarations are required in modules. Selecting this adds the Option Explicit statement to general declarations in any new module.

I also use camel case when I Dim my variables. ThisIsAnExampleOfCamelCase. As soon as I exit the VBA code line if Access doesn’t change the lower case variable to camel case then I know I’ve got a typo.

Leave a Comment