Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

public accessible variables

Status
Not open for further replies.

matrixknow

IS-IT--Management
May 3, 2007
78
I have an few variables strPath and strDir that should be accessed by more then one sub. I thougt I have read something that you can place them above your module or in a class so the are declared ones and accessed by many.

Public Dim strDate As String
strDate = Format(date, "yyyymmdd")

How do I need to do that ?

 
if you want them to be project global in the top of your global module you declare them as so...

Global sVar As String

or if they are to be global to the form module only then

Private sCompanyName As String

at the top of the module code.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Actually the "global" declaration has been replaced by "public". Although both work "public" is preferred.



Scope

Local variables:
Declared in any event, subroutine, or function with the keywords Dim or Static. These variables only exist within the context of the procedure in which they are declared. There is no problem with having local variables in many procedures with the same name. They all will refer to different storage spaces. If there is a conflict in names between a local variable and a more global variable, the local variable always wins. Use Static only if you don't want to lose the value in a local variable between calls to the procedure. Usually you will use Dim.

Module-level variables:
Declared in a form or BASIC module's General Declarations section, with the keywords Dim or Private. Dim is the old syntax; Private is preferred. These variables are accessible from any event, subroutine, or function procedure in the module.

old: Global variables:
Declared in any Basic Module in the project using the keyword "Global". These variables can be accessed and changed by any procedure in the project. It's best to avoid these and use the new "Public" type.

new: Public variables:
These are declared in a form or BASIC module's General Declarations section. They belong to the module in which they are declared, but they can be accessed from any procedure in the project. More about these in a later lesson...

Declaring Variables

within a procedure (local variables):

Dim i As Integer, j As Integer
Static a As Integer, b As Integer, c As Integer

in General Declarations:

Private name As String
Public datafilename As String, ID As Long

WARNING! Do not use:

Dim A, B, C As Integer
 
fair enough, my Access DB is like 5 years old now so I must have some old bad habits :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Keep in mind that in some untrapped Access error situations, you will lose the value in the variable.

I'd suggest using a function in a module, eg:

Code:
Public Function CurrentDate()
    CurrentDate = Format(date, "yyyymmdd")
End Function

Max Hugen
Australia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top