Solved Option Explicit?

I think every time you need to recompile, such as editing a line of code and saving it as the trivial example, Option Explicit gets considered. However, VBA in Access is compile-on-need, not necessarily every time you launch a form or report. If you compiled a module and have not made any changes, it doesn't compile again on launch. (Of course, if you DID edit the module to include "Option Explicit"... that IS a change to the code!)
Bingo!.. That's what I thought. It's a Pre Compiler Directive.
 
Last edited:
I find something has not been declared. I add Option Explicit, and then it does not compile, which you would expect.
Bingo!.. That's what I thought. It's a Pre Compile Directive.

Just to be clear. The Option Explicit directive itself affects only the module you put it in. So, if you take an old db and change the default, nothing in the existing modules changes. You must manually add the directive to every module in the database and then of course, compile and fix all the errors..
 
However, VBA in Access is compile-on-need, not necessarily every time you launch a form or report
Just as an aside, I have a db where I experiment with code, forms, sql etc. must be over 20 years old (or at least old code etc) as it was upgraded from 2003 to 2010 15 years ago.

Now I have moved to 64bit access, if I create a simple test form with a bit of code behind an event and try to open it, the compiler tells me some api’s in another module (which I’m not using) are not 64bit compliant.

Implication being that the whole project is compiled on form open - or perhaps just reviewed?
 
IIRC, by default, the vb.net and vb6 compilers enforce explicit variable declaration?

Without using Option Explicit you could unintentionally declare a new variable by accidentally misspelling an existing variable name. That happened to me when I was learning to write vba code, and I consumed a lot of time trying to figure out what was wrong.
 
IIRC, by default, the vb.net and vb6 compilers enforce explicit variable declaration?
I'm not entirely sure about VB6, but I think it does.
A definite Yes for VB.Net. And it's a compiler option that applies to all files in a project. You don't need to put the Option Explicit statement in any file unless you want a particular behavior for one specific file overriding the global option.
 
However, VBA in Access is compile-on-need, not necessarily every time you launch a form or report.

Although that is the default, it depends on the VBE settings

1753812520214.png


From the VBE Options help:
Compile On Demand: Determines whether a project is fully compiled before it starts, or whether code is compiled as needed, allowing the application to start sooner.

Background Compile: Determines whether idle time is used during run time to finish compiling the project in the background. Background Compile can improve run time execution speed. This feature is not available unless Compile On Demand is also selected.
 
Although that is the default, it depends on the VBE settings

Thanks, Colin. It has been so long since I had to worry about those options that I had forgotten them. Was that always an option or do you recall when that option was introduced?
 
That option has existed since at least A97 (i.e. before the Office VBE was integrated into Access) and has always been on by default.
As mentioned in post #9, Require Variable Declaration was also on by default in A97

1753908007501.png


The VBE was added in A2000 and its Options menu looked almost identical to that in current versions of Office

1753908232961.png


However, as I also mentioned in post #9, from A2000 onwards, Require Variable Declaration became disabled by default in line with Excel & Word.

1753908340674.png
 
That option has existed since at least A97 (i.e. before the Office VBE was integrated into Access) and has always been on by default.
As mentioned in post #9, Require Variable Declaration was also on by default in A97

View attachment 120712

The VBE was added in A2000 and its Options menu looked almost identical to that in current versions of Office

View attachment 120713

However, as I also mentioned in post #9, from A2000 onwards, Require Variable Declaration became disabled by default in line with Excel & Word.

View attachment 120714
So if my project already has code with no declared variables and I turn on require var declaration, and compile on demand is static, its going to throw errors when I launch the app?
 
No. The line "Option Explicit" has to be manually added to existing modules. All the property setting does is automatically include this line in new modules.
 

Users who are viewing this thread

Back
Top Bottom