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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global vs Public Variables

Status
Not open for further replies.

abe6162

Programmer
Sep 7, 2004
24
US
This more of curiosity question. and just to let you know, I am new to VBA

What is difference between global and public variables?

I am working on a project that has multiple forms and modules. The variable needs to keep its value, once assigned, throughout the project. I noticed that the global variable does not seem to work well when I need to use the variable in different modules. If I declare the variable public, it works as I expected.
Thanks!
 
Hi

It is all about scope

Global variables can be "seen" throughout the project

Public variables can be "seen" within the scope they are declared as public eg within a form

Private variables can be "seen" only in the procedure within which they were "Dimmed"

In general the scope of any variable should a small as possible, the greater the scope the more possibility of inadvertant corruption

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hi KenReay,
I am also a little new to Access. I have a Public Variable that I need to be accessable to all users on the front end of an Access db. I have in the Module name "Class 1" this;
Public tempTicket

Then, in my form, I am trying to access it and increment it like this;

Private Sub Combo55_LostFocus()

tempTicket = Format(Date, "yyyymmdd""000""")
tempTicket = tempTicket + 1

JobTicketNumber = tempTicket

End Sub

I realized that doing this on the LostFocus action of a text box is not ideal, becuase, if this worked, it would increment the number every time you went back to the text box (but that is a topic for another thread!).

Anyhow, Public variable "tempTicket", declared originally in the Module, does not increment when the VBA code is run from the form. Any idea why?

Thanks,
-Joe
 
Thanks. I just tried, and I can't figure out how to do that!
-Joe
 
Joe,
I don't think there's anything wrong with the scope of your variable. I think you may be asking for some grief by not explicitly declaring a data type for it, though. Where are you initializing tempTicket, and where are you incrementing? In successive lines, exactly as in your example? If so, seems to me that's why it's not incrementing - you're resetting the value of tempTicket each time the sub runs. Initialize the variable somewhere else, maybe in the form's Open event.

HTH

Ken S.
 
Ken,
Thanks for your input.
Here is what I did;

I used this in the LostFocus property of the first in tab line of text boxes (actually a combo box);

MaxNumber = DLookup("Nz(Max([JobTicketNumber]),Format(Date(),""yyyymmdd000""))", "tblJobTicket", _
"JobTicketNumber>Format(Date(),""yyyymmdd000"") And JobTicketNumber<Format(Date()+1,""yyyymmdd000"")")

JobTicketNumber = MaxNumber

This gives me a JobTicketNumber right away in my Primary Key. This way, I don't even have to worry about working with a Global or Public variable. The JobTicketNumbr value in the table keeps track of the highest value. Best of all, the date adjusted from date to day as well!!!!
Thanks again for your advice.
 
Public variables can be "seen" within the scope they are declared as public eg within a form
Public is the new way to say 'global', they're the same, global is there for backwards compatibility. A Public variable has scope throughout the project.
--Jim
 
Joe,
Glad you got it sorted out. Almost always more than one way to flay that feline.

Ken S.
 
I would like a few things clarified on this subject, please. I want to be sure I understand this correctly. Does this mean that a public variable within a form (value set on the form's OnOpen event) will hold it's value and the value of the variable would be available as long as the form is open?

Also, how would a public variable be set at at module (not form) level when the value is not a constant? For instance, the variable will store the logon ID set/used within the software and that will be attached to each record inserted along with a date/time stamp. Would you call a function within a module (not a form module) during startup, after logon has occurred, that would retrieve the info and set it to the public variable? And then that variable value would be available throughout the project?
Thanks for any thoughts on this.

Linda in MN
 
==> What is difference between global and public variables?
First of all, it depends on where the variable is declared. If the variable is declared in the declarations section of a standard code module, then there is no difference between public and global. That variable will be available to the entire application for the life of the application. Also, there is only one copy of that variable.

However, if the variable is declared in the declarations section of an object (Form, Report, or Class), then it can only be public or private - not global. You cannot declare global variables in objects. The scope is limited to the defining object and only exists as long as the defining object exists. However, it may be referenced outside of the object, provided the object exists, and the variable is identified with a fully qualified object name, such as FormName.Public_Variable or ClassName.Public_Variable. In this case, there will one copy of the variable for each instance of the object in which it's defined.

You may (or may not) find faq705-5319 helpful.

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
==> Public is the new way to say 'global', they're the same, global is there for backwards compatibility. A Public variable has scope throughout the project.
That is true if and only if the variable is declared in the declarations section of a standard code (bas) module. A public variable declared in an object (Form, Report, or Class) does not have project-wide scope.

==> Does this mean that a public variable within a form (value set on the form's OnOpen event) will hold it's value and the value of the variable would be available as long as the form is open?
Yes, it will hold its value as long as the form is open, but it can only be accessible from outside the form by identifying the form by name. FormName.VariableName

Good Luck
--------------
To get the most from your Tek-Tips experience, please read FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top