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

public variable loosing value

Status
Not open for further replies.

flyagain

Programmer
Joined
Jun 13, 2001
Messages
7
I have a public variable 'userid' which is set in 1 place only. It is used throughout the program to update records, but occasionally becomes blank. Is there any known problem with public memory getting overridden (such as not returning recordsets to nothing, thus hogging mem)?
 
If possible, can you declare the variable as
Code:
global
. I'm not sure exactly, but I think a public variable, while it is available to any other module in a project, will lose it's value outside of the module in which it is declared or set. It's a little too early for me to get any clearer than that.
 
You have to check the scope of your function. Declaring them public/global doesn't make much of a difference if you leave the scope. Once you step outside the scope of your module/function/sub a variable is returned to undefined, and if you aren't using OPTION EXPLICIT you are not required to redefine that variable.

Make sure you are employing OPTION EXPLICIT, and double check your scope.



Randall Vollen
National City Bank Corp.
 
If you get a run-time error and reset your code, you cannot rely on the value of any of your variables. If you are really having problems, you could always store your variable in an unbound field on a form (make it invisible?) - which will survive a code reset.

Is your variable a Public variable in a code module - not a form module? If not it should be.
 
The variable is in a code module as 'public' and 'option explicit' is set in all class modules. What is the difference between 'public' and 'global'. The help definition of 'public' is exactly what I am looking for.
 
The following is from the MSDN site:


If you declare a public variable in a Visual Basic for Applications module that is associated with an object, such as a UserForm or a Worksheet object, only the procedures in the module can access the value of the variable.

If you declare a variable with the Public statement in a module that is associated with an object, such as a UserForm and Worksheet object, the variable is only available in that module.

If you want to access a public variable in all procedures for all modules of a project, you must declare the variable in a general module. To insert a general module in a project, click Module on the Insert menu.

 
If you declare the variable as global at the module level (not the procedure level), the variable, once you assign it a value, should be available to all other modules in the project. I have been using this method in my current project and it seems to work well. For instance, I have a module (not a form module) with procedures that use the global variable. I can set the global variable when a user clicks a button on a form. Long after the form is closed, I can call the procedures in the module and the value of the global variable is still set.
 
The variable in question is declared as Public at the module level. Can you explain why a Global declared at the module level would be different?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top