Option Explicit (1 Viewer)

Ilovexfiles

Registered User.
Local time
Today, 15:07
Joined
Jun 27, 2017
Messages
37
Im new to vba. Im halfway through building my database and have realized how important "option explicit" is, which I have not been using, and am still trying to grasp the concept of.

I'm thinking of turning it on now to use moving forward and figure out along the way, or is it imperative that I add it to the coding ive already completed?
Thanks
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 08:07
Joined
Jan 20, 2009
Messages
12,854
Add it everywhere. Then any error in your variable names will be thrown at Compile time instead of waiting until it is run.

You can probably use a Find and Replace across the whole project to change

Option Compare Database

to

Option Explicit
Option Compare Database
 

Orthodox Dave

Home Developer
Local time
Today, 23:07
Joined
Apr 13, 2017
Messages
218
You need to do what Galaxiom says for all existing code. The statement doesn't have to be put into every individual procedure, just at the top of the page of each module for which you have code - under Option Compare Database as in
Code:
Option Compare Database
Option Explicit
Then if you set the VBA Tools as in the attached picture, it will be set automatically for all future code you write.
 

Attachments

  • OptionExplicit.jpg
    OptionExplicit.jpg
    104.4 KB · Views: 89

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:07
Joined
Sep 12, 2006
Messages
15,694
if you do not set "option explicit" then all undefined variables are created as variant types.

Having "rogue" variable makes debugging hard, and can cause unwanted side effevts - as also can incorrect variable typing.

I cannot think of any reason not to require variable calculation. It's probably only an option not to have it, because of the historical links to the original Basic language.
 

Ilovexfiles

Registered User.
Local time
Today, 15:07
Joined
Jun 27, 2017
Messages
37
Thank you, I just set the default to Option Explicit.
I have entered declared variables at the top of module. Do I need to add it to each procedure too? Also the Integers below are actually drop downs.

Option Explicit
Option Compare Database

Dim AdmitDate As Date
Dim AdmitTime As Date
Dim PhysicalorTc As Integer
Dim NeworCds As Integer
Dim ExitDate As Date
Dim ExitTime As Date
Dim Placement As Integer

---

Private Sub ExitDate_BeforeUpdate(Cancel As Integer)
If Me![ExitDate] < Me![AdmitDate] Then
MsgBox "Exit Date cannot be earlier than Admit Date!"
Cancel = True
End If
End Sub
 

Orthodox Dave

Home Developer
Local time
Today, 23:07
Joined
Apr 13, 2017
Messages
218
I have entered declared variables at the top of module. Do I need to add it to each procedure too? Also the Integers below are actually drop downs.
Er ... oh I see. You don't declare variables for items on forms that you are referring to. For example:
If Me![ExitDate] < Me![AdmitDate]
The above(ExitDate and AdmitDate) are not "Variables", but controls on a form.

Have I read you right?
 
Last edited:

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 18:07
Joined
Oct 17, 2012
Messages
3,276
Okay, a quick lesson on variable scope.

If you declare variables in the top section of a module, before any procedures are defined, then they have module-level scope. This means that these variables are visible and usable for all procedures in that module.

If you declare them inside a given procedure, then they are local variables, meaning they can only be seen and/or used by the procedure where they were defined.

If you declare them at the start of the module (keep in mind that the Option statements still need to be first) but add the 'Public' or 'Global' keywords to the declaration, then they're actually available throughout the application. (Public is available in more places, so if you need to use these, get used to using that one.)
 

Ilovexfiles

Registered User.
Local time
Today, 15:07
Joined
Jun 27, 2017
Messages
37
Er ... oh I see. You don't declare variables for items on forms that you are referring to. For example:

The above(ExitDate and AdmitDate) are not "Variables", but controls on a form.

Have I read you right?

Hi yes, I am referring to the "field names" in the code and have gotten compile errors everywhere stating: ""The member already exists in an object module from which this object module derives"
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 18:07
Joined
Oct 17, 2012
Messages
3,276
You really don't want to declare variables with the same names are your fields or controls. In fact, I'm about 99% certain that that is why you're getting the compile error.

Hell, I make a point of ensuring my controls don't even share their names with fields, and Access can generally handle that situation fine.

My first recommendation would be to delete or rem out all those declarations you made at the start of the form module. You only need to declare VARIABLES, not existing controls and fields.
 

Cronk

Registered User.
Local time
Tomorrow, 08:07
Joined
Jul 4, 2013
Messages
2,774
Hell, I make a point of ensuring my controls don't even share their names with fields, and Access can generally handle that situation fine.

Me too but I do make it such that it is easy to identify the control if I am referring to it in code.

Field name: LastName
Control name: txtLastName or cboLastName
Variable name: strLastName
 

Users who are viewing this thread

Top Bottom