Full Version: variable scope when starting from another form
UtterAccess Forums > Microsoft® Access > Access Forms
Dani50
Hello,
I have declared a module level variable in a form module to carry a value between procedures and all worked fine whilst I was just opening the form but If I open the form from another form it ceases to work. Could someone please explain this aspect of scoping.
Dani.
ace
When and how is the module level variable set?
arry a value between procedures in the same form module or another form's
module?
Once you set the value of a form level variable it should persist until you
change it, an unhandled error occurs or the form is closed.
If you have declared the variable as Public then you should be able to
reference it via the Form's collection.
Forms("FormWithVariable").VariableName.
Dani50
Hi Ace, Thanks for replying.
That is what I thought. It is only used in the form in which it is declared as private underneath the option explict line. It works fine if I just open the form from the database window but the problem occurs when I open it from a button on another form that then closes.
I have now changed the declaration to Public and used 'Me.VariableName' to set the value which now works, I can retrieve the value just with the VariableName. So I am glad to have a solution but I am curious as to know why the way the form was opened made a difference.
Thanks,
Dani.
ace
I don't see why the way the form is opened should make a difference.
That is why I asked when the variable is assigned a value.
nother thing is that if the variable is only referenced from the form it
is declared in it should not have to be declared as Public.
Dani50
I absolutely agree. It is given a value in the form's on_current event.
vtd
A Module-level variable is available to all procedures within the Module. If the module also has procedure-scope variables with the same name, VBA will use the "closest" declaration unless the Qualifier is used with the Variable. However, it is never a good idea to have different variables with same name in overlapping scopes.

Please post the code for the CommandButton that opens this Form and describe what happens ... Unfortunately, " ...it ceases to work ..." does not give any clue for potential respondents.

Do you get any Access / VBA error? If you do, error number and error description?

Have you tried to trace the code to ensure that the Variable is assigned a value (different from the default value) before it is actually used?
Dani50
I have now found the source of the problem so I thought I would let you know.
My module level variable is called gstrFileName. There was a similar but different procedure level variable called strFileName. I have now removed the latter and the problem of losing scope has gone. I put the 'g' in front of a module level variable so I don't accidently give the same name to a procedure level variable and this distinction was sufficient when opening from the database window but not apparently from another form. I find it a bit worrying really.
Thanks for your input.
Dani.
vtd
You're welcome ... Glad you worked it out.
Since you are using Access 2000 (which was bugsy on the original release), you need to make sure that your Access installation (and Windows) are fully patched with all Service Releases & Serice packs ...
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.