Public Variable not so Public

funderburgh

Registered User.
Local time
Today, 17:44
Joined
Jun 25, 2008
Messages
118
I am developing a small application and I have public functions and variables defined in a module.

My first feature works well: Form1 opens Form2 which puts a value in pubVar1. Form2 opens Form3 which uses the value of the public variable.

I am having a problem with my second feature using the same forms. Form1 opens Form2 which puts a value in pubVar1 and then closes. Form1 trys to use the variable, but the value is null.

I can't see what I am missing. Any help is appreciated.

PS The forms evoke the public functions contained in the module successfully.
 
Is the variable declared in a STANDARD module (not form or report module)? It has to be in a Standard Module to be able to be accessed from outside of the form or report.
 
Or if you don't want to create a seperate module for setting your variable, create a Public Function within your form and use the function's paramater to set your value, like this:

Public Function SetpubVar1(newPubValue as variant)
pubVar1 = newPubValue
End Function
 
Variables declared as Public in Form class modules are accessible from outside that module.

However, we need more information specifically about where the variables and code are and what is open at the time.
 
Well, I guess so, using the

Forms("FormName").VariableNameHere

So, I forgot about that usage. My bad.
 
  • Like
Reactions: SOS
Well I am embarrassed to say I can't figure out how to check the Type of my module, but the icon indicates it is not a Class module. The module is filled with functions that I have cribbed for my application and I am calling them successfully from my forms' code. But the forms I am making the calls from are the forms where my public variable is working. I lose the value when I return to the originating form. Thanks for the attention.
 
Values of global variables are normally persistent until you close the application. There might be a piece of code that resets the value somewhere? To answer your other question, if it's not a class module then it's just a module.
 
Another possibility is that you have the ‘global’ variable declared both in the standard module and in the class module. That will not raise an error but you would be writing to one of the variables and reading from the other.

Do a project wide search on the variable name and see if you have a duplication.

Failing that it might be an idea to post your application so someone can have a look at it.
 

Users who are viewing this thread

Back
Top Bottom