Variable not defined?

T588

Registered User.
Local time
Today, 01:21
Joined
Feb 3, 2003
Messages
65
I've read that you should use the "Option Explicit"
on almost every Function or sub to eliminate undefined variables.

I wrote the code below before I added "Option Explicit" and it worked.

Now, using "Option Explicit" - I get an error: "Variable not defined"

ProductName = Cleared
ShiptoAddress = Cleared
ShiptoCity = Cleared
ShiptoState = Cleared

How should "clear" be defined as a variable?

What will work with the above example?

I never defined clear before. Can I use "String"?

Any info appreciated.

Thanks

Vince
 
T588 said:
I've read that you should use the "Option Explicit"
on almost every Function or sub to eliminate undefined variables.

It goes at the top of each module and should be the very first line.

You can turn it on in the options.


ProductName = Cleared
ShiptoAddress = Cleared
ShiptoCity = Cleared
ShiptoState = Cleared

How should "clear" be defined as a variable?

What will work with the above example?

I never defined clear before. Can I use "String"?

In this case you would use a string.

I don't know how much you know so pologies in advance if any of this is not helpful (one day it might be to someone :))

Before defining a variable, you should think about which level you are declaring it at.

You can variables at sub-routine level, form level, and at (for want of a better word) database level.

These variables can be created using the Dim statement.

i.e. Dim Cleared

This would create a variable called Cleared which would be a variant meaning that it can hold different types of value (numerical, string, date, etc.)

Variants, however, consume memory, so, if a variant is not necessary, it's better to dimension the variable to be of the type that it will hold.

i.e. Dim Cleared As String

Again, variables can be confusing when looking at code because it isn't always clear what sort of data type the variable holds - or has been created to hold - and so it's a better practice to use a prefix on variable names.

i.e. Dim strCleared As String

Straight away, when looking at the code, when strCleared is used, you'll know immediately what data type a variable is.


If you dimension a variable within a sub-routine then it is only 'active' until the end of that sub-routine - if the sub-routine is called again, then the variable is recreated as opposed to reused.

If you dimension a variable within a form or report then it is only 'active' while the form is open - if the form/report is opened again, then the variable is recreated as opposed to reused.

At a database level, we would declare a variable as Public so that it survives for the duration of the database being open and can be used within any form, report, or module.

i.e. Public strCleared As String


On occasion, if you have a variable that is 'active' through a sub-routine or at form/report level then you find occasions where you would want Access to remember its value even after the sub-routine is complete, or the form/report has been closed. To do this, you would dimension the variable as Static.

i.e. Static strCleared As String




With the code you have given:

Code:
ProductName = Cleared
ShiptoAddress = Cleared
ShiptoCity = Cleared
ShiptoState = Cleared

I wouldn't know exactly where you should put the Dim Cleared As String statement as you have given four situations and not, I'm presuming, your actual code - if that is your actual code then all four elements(ProductName, ShiptoAddress, ShiptoCity, and ShiptoState) would be receiving the same value.
 
Appreciate it - alot of good info in that lit'l answer -Thanks
and I wouldn't take offence regardless of how much I "thought" I knew - I can never know enough!
Thanks Again,
Vince
 

Users who are viewing this thread

Back
Top Bottom