Public Variables (1 Viewer)

jayclifford

Registered User.
Local time
Today, 00:32
Joined
Jul 23, 2013
Messages
16
I want to make a Form name public to all procedures in a module (maybe all modules), without repeating the Dim statement or its name.
I envisioned the code to look something like:

Code:
Public Function SetVar1
Dim Fr1 as String
Set Fr1 = "Form_Name_Form1"  'This Form is based on a Query
End Function
Then all uses of Fr1 would refer to the Form "Form_Name_Form1" anywhere in the module.
Questions:
Does this Function have to go in the Declarations section of the Module or can it go anywhere in the Procedures section?
Excuse my ignorance, but how do I get into the Declarations section of a Module to add code?
Can it go in a Form Module or a Class Module?

Please excuse my naming conventions.

Jay
 
Last edited by a moderator:

Isaac

Lifelong Learner
Local time
Today, 00:32
Joined
Mar 14, 2017
Messages
8,774
I believe if you simply make it a public function (as you have) and put it in the regular section of a standard module, then you may then use it and find it useful to refer to everytime you need to refer to that form.
HOWEVER...probably not quite the way you're doing it. Based on what you have so far, the closest you are to anything useful is to change this line:
Set Fr1 = "Form_Name_Form1
to:
Set Fr1 = "Name_Form1

... and then refer to the function (SetVar1) anytime you needed the string version of the form's name. (If you find that typing the function name is more convenient than typing a one-word form name(?))..
But this would NOT work to type SetVar1 and expect to see intellisense and actually be referencing the Form_formname module the same way as when you type Form_Formname[dot]
...is that what you're really after? If so, then try something more like this:

Code:
Function TheForm() As Form
Set TheForm = Form_Form1
End Function

Sub test()
MsgBox TheForm.Name
End Sub
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:32
Joined
Feb 19, 2002
Messages
43,196
I'm trying to figure out how this would be useful. To reference a form's objects, the form MUST be open. In my apps, the Login form always stays open. When you enter your credentials and click Login, the code valiates your credentials and if they are valid, it hides the form - Me.Visible = False and then opens the switchboard form. Throughout the app, I reference controls on the LoginForm. I do it by using the whole name but you could define a global variable in a standard module's header as a public Constant
Code:
Option Compare Database
Option Explicit

Public Const frmName = "MyFormName"

However this is a string. If you want to define it as a form, you can but you'll need a separate step to give it a value and you should do that in the Open event of the form you want to reference.
 

jayclifford

Registered User.
Local time
Today, 00:32
Joined
Jul 23, 2013
Messages
16
Thank you for your quick response.

As it turns out the entire name of the Form is "Form_Name_Form1" (this is not the real name of my Subform), so that I can lump all forms together and subforms together in the forms listing.
My Thought was to declare all my Subforms at once and just refer to the short name in all procedures.
This is what I am trying to accomplish. I have a main Form with several Subforms. I use a Control Box to pick the Subform I want to make visible by using If...Then...Elself statements to get the correct Subform (this works great). By using the Fr# short form of the Subform I would be able to use that short from to make various Subforms visible or not visible, in addition to calling one of the Subforms.

Is any of this making sense?
Here is some of the code, it works.
Code:
Private Sub Trans_Date_GotFocus()
    Dim Form1 As Form
    Dim SubF1 As Control
    Dim SubF2 As Control
   
   
    Set Form1 = Forms("Form_Trans_Form1")
    Set SubF1 = Forms("Form_Trans_Form1").Subform_Bill_Power_Form1
    Set SubF2 = Forms("Form_Trans_Form1").Subform_Bill_Rent_Form1
   
    If Forms!Form_Trans_Form1.Trans_Entity_ID = 4 Then
        SubF1.Visible = True
        SubF2.Visible = False
    ElseIf Forms!Form_Trans_Form1.Trans_Entity_ID = 14 Then
        SubF2.Visible = True
        SubF1.Visible = False
    End If


End Sub
I'm really want to Dim as a String not a control.

Jay
 
Last edited by a moderator:

Isaac

Lifelong Learner
Local time
Today, 00:32
Joined
Mar 14, 2017
Messages
8,774
in that case just pay attention to the first half of my post and discard the rest.

try this

Code:
Public Function fr1() as string
fr1 = "Form_Name_Form1"
End Function

Now anytime you need to refer to Form_Name_Form1 as a string, you just type fr1
 

jayclifford

Registered User.
Local time
Today, 00:32
Joined
Jul 23, 2013
Messages
16
in that case just pay attention to the first half of my post and discard the rest.

try this

Code:
Public Function fr1() as string
fr1 = "Form_Name_Form1"
End Function

Now anytime you need to refer to Form_Name_Form1 as a string, you just type fr1
I want to thank all who replied for your promptness and support.

Is it that easy, just put commas after string and add more names as strings?
What does [/CODE] mean or do?

Jay
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:32
Joined
Feb 28, 2001
Messages
27,122
If you REALLY wanted to do this right and directly reference the form, you do NOT use a string. Declare FR1 as an Access.Form object, then

Code:
Public Function fr1() as Access.Form
Set fr1 = Forms("Form_Name_Form1")
End Function

Note that this indirect referencing method is FRAUGHT with peril. (It's been a long time since I had a chance to use that phrase...) in that if you have an object set to the form and the form closes, the next reference to the FR1 variable will crash Access or at the very least trigger the debugger with (I think) an "Error 91 - Object not set" error. AND if you were passing the name and tried to use Forms(formname) then the same problem occurs if the form isn't open at the time.

Basically, any design that tries to dynamically link to forms has GOT to be carefully coded to include tests for non-existence (technically, unloaded status) for targeted forms.
 

jayclifford

Registered User.
Local time
Today, 00:32
Joined
Jul 23, 2013
Messages
16
If you REALLY wanted to do this right and directly reference the form, you do NOT use a string. Declare FR1 as an Access.Form object, then

Code:
Public Function fr1() as Access.Form
Set fr1 = Forms("Form_Name_Form1")
End Function

Note that this indirect referencing method is FRAUGHT with peril. (It's been a long time since I had a chance to use that phrase...) in that if you have an object set to the form and the form closes, the next reference to the FR1 variable will crash Access or at the very least trigger the debugger with (I think) an "Error 91 - Object not set" error. AND if you were passing the name and tried to use Forms(formname) then the same problem occurs if the form isn't open at the time.

Basically, any design that tries to dynamically link to forms has GOT to be carefully coded to include tests for non-existence (technically, unloaded status) for targeted forms.
Thank you for your help. Yes, I see that this could be a hazard to the database.

Jay
 

Users who are viewing this thread

Top Bottom