Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set a constant global reference to a form in public module?

Status
Not open for further replies.

inso18

Technical User
Dec 30, 2006
147
IL
Hello.

I need help in setting a public constant global reference to a form in public module for convinience, so that all form modules could use it.

I've tried
Global Const frmParameters = Forms!frmMain!sfrmParameters.Form

I understand this is not the way as this is not a variable but an object reference.

Please help
thanks a lot, inso18
 




Hi,

What APPLICATION are you running?

Declare as a Public Object Variable

Set to the Form Object in the open event for the Workbook or Document,


Skip,

[glasses]Just traded in my old subtlety...
for a brand NUANCE![tongue]
 
VB and VBA in a Nutshell by Oreilly states:
Code:
Const Statement
Named Arguments
  No
Syntax
  [Public|Private] Const constantname = constantvalue
constantname
  Use: Required
  The name of the constant.
constantvalue
  Use: Required
  Data Type: Numeric or String
  A constant value, and optionally, arithmetic operators. Unlike variables, constants must be initialized.

....[b]The constantvalue expression can’t include[/b] any of the built-in functions or [b]objects[/b], although it can be a combination of absolute values and operators. The expression can also include previously defined constants.
 
I'm running ms-access application, and I need sfrmReports to a constant object reference to Forms!frmMain!sfrmReports.Form, so when I will type

sfrmReports.ctl1.value I will get
Forms!frmMain!sfrmReports.Form!ctl1.Value
 
You can not declare a constant as an Object. From Help:
constant
A named item that retains a constant value throughout the execution of a program. A constant can be a string or numeric literal, another constant, or any combination that includes arithmetic or logical operators except Is and exponentiation.

Or, as electricpete noted:

Data Type: Numeric or String

What is wrong with simply declaring the form as an object, and then setting it to be your form?
Code:
Option Explicit

Public myForm As Object

Sub whatever()
  Set myForm = UserForm1
  myForm.Show
End Sub

faq219-2884

Gerry
My paintings and sculpture
 





I seem to hear an ECHO echo echo echo

Skip,

[glasses]Just traded in my old subtlety...
for a brand NUANCE![tongue]
 
Thanks for your posts, sorry it took me long to reply

fumei, the reason I need this, is that I use certain forms reference a lot in other forms, so each time I need to type:

Fomrs!frmMain!sfrmSecondary.Form!ctl...

and it makes the app more difficult to read for people who will be maintaing it later on..
 
Oh, I understand the need perfectly well. It makes sense.

So....that is how you do it. Make a global form object, and set it. It can be used anywhere it has Scope.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks fumei.
I've been struggling on how exactly to do it.

Can you explain how this can be done?
 
In a standard code module:
Code:
Public frmParameters As Access.Form
In the Load event procedure of frmMain:
Code:
Set frmParameters = Forms!frmMain!sfrmParameters.Form
In the Unload event procedure of frmMain:
Code:
Set frmParameters = Nothing

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks PHV. Sorry for being picky but is it possible to create a global reference to the form, so that I wouldn't have to define on each form's Load procedure the same set?
 
define on each form's Load procedure the same set
????
I suggested only frmMain ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,
I have many forms that use this reference. Is the only way is to set each reference on the OnLoad of each form, or can it all be set globally somehow?
 
Is the only way is to set each reference on the OnLoad of each form
No.

You misunderstood what PHV is saying.

In order to reference the actual subform the subform needs to be open. So when the mainform opens you can set the global variable. You only set the variable one time (actually every time the Main form opens. No one has said anything about the OnLoad of each form.).
Now all the other forms can reference the global variable.

I would recommend doing what PHV is saying because it looks like you are speaking about a specific instance of the subform. However, you can also do this in a standard module.

public frm as New FORM_frmParameters

now you can use frm anywhere. This can get dicey if you have multiple instances of the subform open or the frmParameters open and it is not a subform. You may end up return values from the wrong form instance.
 
Actually I lied. I had to think for a second. If you do this
public frm as New FORM_frmParameters
You can use frm to return any properties of frmParmaters. However, this is a unique instance (hidden) and the values you return will not be from the current record of any of the visible instances of the subform (unless you force "frm" to synch its record to a visible instance.) Bottomline, you are probably better off explicitly setting the global variable to the sub form instance as PHV suggests.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top