Declaring a public variable

B

Baby Face Lee

Hi there,
I'm sorry this is a dumb question but I'm in a bit of a muddle as I don't
really know what I'm doing!!!...
To save constantly writing out the full path to each of a number of controls
on a subform, I thought I'd assign each name to a variable. An example of
the control name is 'Forms!frm_Arrangements!frm_ProgressLogSubform.Form.chk9'.
What I really want to do is make the variable public and declare the values
publicly but instead I have to assign a value to the variable within each
procedure which makes it all rather lumpy. I've tried making each name a
Constant instead but as there are values to be assigned to some of the
controls, there's an error 'cos you can't change a Constant (otherwise it
wouldn't be constant!! - I understand that bit at least).
So, is there a neater way of declaring and specifying the names of these
controls for use throughout the database project? I've been declaring the
type as String and then I tried Control but that seemed to make matters worse!
Sorry for the waffle but any help would be very much appreciated.
Regards,
Lee
 
G

Graham Mandeno

Hi Lee

You were on the right track with Control, but bacause it is an object (not a
simple variable) you must use a Set statement:
Set MyGlobalVariable =
Forms!frm_Arrangements!frm_ProgressLogSubform.Form.chk9

Of course, it will only be valid when frm_Arrangements is open, so you can
set it and clear it in the form's Load/Unload events:
Set MyGlobalVariable = Me!frm_ProgressLogSubform.Form.chk9
and
Set MyGlobalVariable = Nothing
 
B

Baby Face Lee

Thank you so much, that's a real help.

Lee

Graham Mandeno said:
Hi Lee

You were on the right track with Control, but bacause it is an object (not a
simple variable) you must use a Set statement:
Set MyGlobalVariable =
Forms!frm_Arrangements!frm_ProgressLogSubform.Form.chk9

Of course, it will only be valid when frm_Arrangements is open, so you can
set it and clear it in the form's Load/Unload events:
Set MyGlobalVariable = Me!frm_ProgressLogSubform.Form.chk9
and
Set MyGlobalVariable = Nothing
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Baby Face Lee said:
Hi there,
I'm sorry this is a dumb question but I'm in a bit of a muddle as I don't
really know what I'm doing!!!...
To save constantly writing out the full path to each of a number of
controls
on a subform, I thought I'd assign each name to a variable. An example of
the control name is
'Forms!frm_Arrangements!frm_ProgressLogSubform.Form.chk9'.
What I really want to do is make the variable public and declare the
values
publicly but instead I have to assign a value to the variable within each
procedure which makes it all rather lumpy. I've tried making each name a
Constant instead but as there are values to be assigned to some of the
controls, there's an error 'cos you can't change a Constant (otherwise it
wouldn't be constant!! - I understand that bit at least).
So, is there a neater way of declaring and specifying the names of these
controls for use throughout the database project? I've been declaring the
type as String and then I tried Control but that seemed to make matters
worse!
Sorry for the waffle but any help would be very much appreciated.
Regards,
Lee
 
J

John Nurick

It sounds as if you're just trying to save keystrokes when writing code.
If so, you can use constructions like this:

With Forms("frm_Arrangements").Controls("frm_ProgressLogSubform").Form
.Controls("chk8").Value = True
.Controls("chk9").Value = True
[...]
End With

(this also works with the short-form Forms!formname!controlname
notation).

Also, if the code is a form's module, you can use the Me keyword to
refer to controls on that form, e.g.
Me!frm_ProgressLogSubform.Form!chk9.Value = True

And you can use constructions like this to mimic Visual Basic's control
arrays:

Dim j As Long

For j = 1 to 5
Forms("frmName").Controls("chk" & cstr(j)).Value = True
Next

Finally, of course, there's copy-and-paste<g>
 

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