Solved

Option Compare Database and Option Explicit

Posted on 2013-01-31
10
726 Views
Last Modified: 2013-02-04
On a number of the modules behind my forms it starts with:
Option Compare Database
Option Explicit

From what I understand this seems to be a good practice for the beginning of all modules with this though I am not 100% sure why. I see Option Explicit as a way to for the module to pick up on any typos in declarations.

I suppose first I would like to get clarification on the purpose of this code and also know if it is worth opening all the modules and inserting this if it is not already there.
0
Comment
Question by:Shawn
  • 6
  • 4
10 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 38841196
"and also know if it is worth opening all the modules and inserting this if it is not already there."
Just an FYI ... for a *new* Module ... you can have it automatically add Option Explicit:

Auto add Option Explicit
0
 
LVL 75
ID: 38841206
From Help:
"When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at compile time.

Note   Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear"


Option Explicit is DEFINITELY best practice.
0
 
LVL 75
ID: 38841217
To not reinvent the wheel ... look up Option Compare in VBA help ... it gives a full explanation of what this means ... basically determining the comparison method to use when string data is compared.
0
 
LVL 1

Author Comment

by:Shawn
ID: 38841261
ok, pretty sure it's clear now.

The wierd thing is as you know I've set up vbWatchdog (amazing btw) and on my login form I added this:
Private Sub Form_Load()
Call ErrEx.Enable("OnError")
End Sub

When opening the db and the login form loads I got the error on load...file not found.
I noticed in this module I did not have..."Option Compare Database and Option Explicit", added it. Closed the db, opened and presto, no more error.

I can't figure out the relation between this error and the option compare/explicit.
0
 
LVL 75
ID: 38841283
Humm ... must have to do with Option Compare. W/O that statement, the default is Option Compare Binary - not Database .... but still.

You might want to run this by Wayne ...
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Author Comment

by:Shawn
ID: 38841417
ok, will do. Bug is gone but bizarre. I'll let you know what he says.
0
 
LVL 75
ID: 38841442
yes ... please do.
thx
0
 
LVL 1

Author Comment

by:Shawn
ID: 38841567
doesn't seem there is a relation but he confirms a suspicion I've had for a while...

Wayne's response: "That error is quite a well known indicator that the module behind the form has somehow become corrupt (specifically, the compiled form of the module).  It's not a problem specific to vbWatchdog though, just a minor problem with the VBA compiler itself that sometimes occurs.

Another way to fix problems like that, is to use the decompile switch in Access, which clears out all the compiled code in your project, ready for re-compilation."

As I have decompiled recently I think I'll opt to redo the form. It's only a small one.
0
 
LVL 75
ID: 38841601
Well, this makes sense.  Corruption is certainly a possibility.

I would have no fear of decompiling again ... just make a back up first.
0
 
LVL 1

Author Comment

by:Shawn
ID: 38852606
decompiled and no error as of yet. thx!

if you have a minute :)
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_28017619.html#a38847262
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now