Access 2003 Public Variables

G

Guest

I am trying to use a public variable in Access 2003 to pass a string from one
form to another. To keep things simple, I've created 2 test forms to
demonstrate the problem.

First form (frmSetName) has the following code:

Option Compare Database
Option Explicit
Public gstrName
'
Private Sub cmdSetName_Click()
gstrName = "John Doe"
MsgBox "gstrName has been set to " & gstrName
DoCmd.OpenForm "frmGetName"
End Sub

Second form (frmGetName) has the following code:
'
Option Compare Database
Option Explicit
'
Private Sub cmdGetName_Click()
MsgBox gstrName
End Sub

Problem: When I click the cmdSetName button in the first form, the public
variable is assigned the value "John Doe", the variable's value is displayed
in the message box, then the second form is opened. When I click the
cmdGetName button in the second form, however, the message "variable not
defined" appears.

Aren't Public variables supposed to be available to all modules?

Can anyone help?

Thanks, Christine
 
L

Lance McGonigal

Not sure what you're trying to do but have you thought about referencing the
object on the other form or maybe storing the value in a table and
extracting it with the dlookup function or a wrapper on the dlookup? I do
this a lot and it's pretty simple.

blessings

"Christine in New Jersey" <Christine in New
(e-mail address removed)> wrote in message
news:[email protected]...
 
D

Dan Artuso

Hi,
Your variable has to be declared in a standard module, not the form's module.
The way you have it, it's only visible to the procedures in your form.
 
D

david epsom dot com dot au

The form module is a class. Public variables of the class are
exposed as properties of the class:

msgbox Form_FrmSetName.gstrName

The object name is "Form_" and the name of the form.

(david)

"Christine in New Jersey" <Christine in New
(e-mail address removed)> wrote in message
news:[email protected]...
 
G

Guest

Thanks, Dan.

I was wondering whether the problem could be that variables declared in
forms cannot be "Public" and you confirmed that for me.

Thanks also, Lance. I will use your approach, since the data I want to use
is stored in a form field.

Christine
 
T

tina

is there any advantage/disadvantage to declaring a public variable in a form
module vs a standard module, when you need to refer to the variable in
another form?
tia
 
R

Rick Brandt

tina said:
is there any advantage/disadvantage to declaring a public variable in a form
module vs a standard module, when you need to refer to the variable in
another form?

As long as the form where the variable is declared will always be opened when
the other form needs to access it there would be little difference.
 
D

Dirk Goldgar

david epsom dot com dot au said:
The form module is a class. Public variables of the class are
exposed as properties of the class:

msgbox Form_FrmSetName.gstrName

The object name is "Form_" and the name of the form.

You can also get at public variables in a form's module as properties of the
form object, like this:

Forms!frmSetName.gstrName

I prefer using a reference to the form object to using the class name,
because (a) if the form happens not to be open, referring to its class
module this way will cause it to be opened, but hidden, which may not be
what was intended; and (b) if there could be multiple instances of the form
open, it's an open question which one you're going to get. I expect it that
which one you'll get is in fact determinable, but is that the one you
wanted?
 

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