Global Variables

L

L-Ess

While I'm not entirely new to Access, I'm still near the
lower part on the learning curve.

My questions is - is there any way to set a Global
variable that persistent whereby any form can use its
current value. What I'm trying to do is have any one of
five forms where when opened, register its form name to
the global value and have a sixth form easily
differentiate (via the Global variable) which of the five
forms is currently opened. To clarify, if form#2 opens,
it sets the Global variable to the string
value "Form#2". Form #6 when opened queries the Global
value to see that Form#2 is currently active. When
Form#2 closes and Form#5 opens, then registration is
repeated and Form#6, when opened concurrently again
queries the Global variable to see that Form#5 is the
currently active form (of the five forms).

Hope this isn't confusing. Any help would be greatly
appreciated (I'd also guess there's a more classic way to
accomplish this - maybe via the application/active form
command or such).

Thanks!
 
K

Ken Snell

To do what you ask, create a regular module (select modules in the database
window and create a new one). Type the following in that module:

Public VariableName As String


That makes VariableName variable available throughout the database. Note
however that ACCESS can "reset" global variables if an unhandled error
occurs when the code runs. As such, another way to do this that avoids this
problem is to open an invisible form that has a textbox on it, and write the
value into that textbox when a form is opened; then any other form can see
it.
 
D

Dirk Goldgar

L-Ess said:
While I'm not entirely new to Access, I'm still near the
lower part on the learning curve.

My questions is - is there any way to set a Global
variable that persistent whereby any form can use its
current value. What I'm trying to do is have any one of
five forms where when opened, register its form name to
the global value and have a sixth form easily
differentiate (via the Global variable) which of the five
forms is currently opened. To clarify, if form#2 opens,
it sets the Global variable to the string
value "Form#2". Form #6 when opened queries the Global
value to see that Form#2 is currently active. When
Form#2 closes and Form#5 opens, then registration is
repeated and Form#6, when opened concurrently again
queries the Global variable to see that Form#5 is the
currently active form (of the five forms).

Hope this isn't confusing. Any help would be greatly
appreciated (I'd also guess there's a more classic way to
accomplish this - maybe via the application/active form
command or such).

Thanks!

In addition to what Ken Snell mentions, note that if, say, Form#2 is
currently active and you click a button that opens Form#6, then in the
Open event of Form#6, the value of Screen.ActiveForm.Name will be
"Form#2". Whether this is useful to you or not depends on the
circumstances in which Form#6 is opened.
 
G

Guest

-----Original Message-----
To do what you ask, create a regular module (select modules in the database
window and create a new one). Type the following in that module:

Public VariableName As String


That makes VariableName variable available throughout the database. Note
however that ACCESS can "reset" global variables if an unhandled error
occurs when the code runs. As such, another way to do this that avoids this
problem is to open an invisible form that has a textbox on it, and write the
value into that textbox when a form is opened; then any other form can see
it.

--
Ken Snell
<MS ACCESS MVP>




.
Ken, Dirk:

It works!!! I thought the Global keyword was supposed to
do what the Public keyword is doing. I tried the using
the Public keyword before, but was mistakenly trying to
use it and the Global keyword in a Class module vs a
Standard module.

I tested your suggestion by having Forms #1 thru #5 each
set the PublicVariable to a string to indicate its
formname (i.e. PublicVariable = "ImForm#1") when it opens
via the form's OnOpen Event. I would also have a command
button on the form which opens one of the other forms. I
then put breakpoints in each of the OnOpen Events of the
forms to observe the current value of PublicVariable.
Its persistent and does exactly what I want.

Thanks a million to you both. You've bumped me up a
notch on the access learning curve.

Thanks!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top