Option Explicit doesn't seem reliable

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

I have a database with one module. I have set Option Explicit at the top.
I expected this to mean that I would get an error if I used a variable that
I hadn't declared. It turns out it's not working. I found this out by
copying the database to another location and renaming it. When I added my
first little bit of code change, I got a compile error because a variable
hadn't been defined. I verifid that it was possible to use new variables
without declaring them in the original database. In the new database all
undeclared variables are caught. (I found this about be declaring the
variable from the first error, and it found another one.) The new copy is
less than an hour old. All I did was make a new module with one tiny
string function in it. The new module was also EXPLICIT. I deleted the
whole module just to try to isolate the cause of the changed behavior.

Does anyone have an idea about what's going on? Why is EXPLICIT not working
in my first database? Why did it begin to work in the new copy of it?

Option Compare Database
Option Explicit
 
It could be that your variable name is also a function or method name. I
have seen lots of programmers that don't realize that date is a function
name.
 
The first variable was li_contract. The second was gb_stats. Anyway, this
wouldn't account for the behavior being different between the two virtually
identical databases.
 
I've never found Option Explicit to be unreliable. One thing I have found in
recent versions of Access, though, is that the VBA editor doesn't always
detect the fact that code needs to be recompiled if I edit code without
moving off the edited line. This could possibly be what you are seeing, as
the undeclared variable will not be detected until VBA attempts to compile
the code.
 
Yeah, I've found that about the VBA compiler too. But in the process of
verifying that the behavior was different in the two db's, I definitely
compiled successfully, after adding yet another undeclared variable.
Compiling was how I verified.
 
Back
Top