Declare / define Form variable for module (Access2007)

AOB

Registered User.
Local time
Today, 14:11
Joined
Sep 26, 2012
Messages
621
Hi,

Is it possible (and how...) to declare a module-specific form variable (or any variable for that matter) at the top of said module, so it doesn't need to be set at the start of each subsequent procedure?

I have a module of code specific to one form with a number of procedures, each one of which requires me to Dim / Set the form variable. It would be much neater if I could do it once at the start.

What should the syntax be please?

Thanks

AOB

Code:
Option Compare Database
Option Explicit
 
Public Sub Populate(lngParameter As Long)
[INDENT][COLOR=red]Dim frm As Form[/COLOR]
[COLOR=red]Set frm = Forms("frmDetails")[/COLOR]
' etc etc...
 
[/INDENT]End Sub
 
Public Sub Validate(lngParameter As Long)
[INDENT][COLOR=red]Dim frm As Form[/COLOR]
[COLOR=red]Set frm = Forms("frmDetails")[/COLOR]
' etc etc...
 
[/INDENT]End Sub
 
I would write a property get . . .
Code:
Private Const FN As String = "frmDetails"

Private Property Get frm as Form_frmDetails
[COLOR="Green"]'  Exposes frmDetails locally
   'opens the form if it isn't already open[/COLOR]
   if not currentproject.allforms(FN).isloaded the docmd.openform FN
[COLOR="Green"]   'always returns a reference to it[/COLOR]
   set frm = forms(FN)
End Property
. . . so now other code in this module have access to frm and you can do . . .
Code:
Public Sub Populate(lngParameter As Long)
   with frm
[COLOR="Green"]      'use the property reference[/COLOR]
   end with
End Sub
 
Or, you can just dim it after the Option Explicit and before the first procedure. If it is declared as Private, its scope will be limited to the module where it is defined.
Code:
Option Compare Database
Option Explicit
Private var1 as String
Private var2 as Variant
etc.
Private firstsub()
 
Thanks guys!

I'm not very familiar with Properties just yet so would feel more comfortable declaring it (frankly, I'm just not sure how or where to refer to the property to assign the form to the variable)

I know how to declare a public form variable but I still need to assign that variable a value before I can use it. I guess my question is, how do I declare a 'constant' public form variable (preferably in a separate module) such that I can refer to that variable throughout the remaining code without having to initialise it every time?

Something along the lines of :

Code:
Option Compare Database
Option Explicit
[COLOR=red]Public frmDetails As Form = CurrentProject.AllForms("frmDetails")[/COLOR]

(Obviously, the above doesn't work, I'm just using it to illustrate roughly what I'm trying to achieve...)

Thanks

AOB
 
A member of the AllForms collection is not an Access.Form object it is an Access.AccessObject.

To return an Access.Form as if it were a constant, so guaranteed to be present even if it happens to be closed when you reference it, you will have to run code, and so you have two choices . . .
1) use a Property Get
2) use a Function

I've shown the Property Get. The function would look almost the same . . .
Code:
Private Const FN As String = "frmDetails"

Private Function GetFormDetails as Form_frmDetails
[COLOR="Green"]'  Exposes frmDetails locally
   'opens the form if it isn't already open
[/COLOR]   if not currentproject.allforms(FN).isloaded the docmd.openform FN
[COLOR="Green"]   'always returns a reference to it[/COLOR]
   set GetFormDetails = forms(FN)
End Property
. . . but a function is more commonly used to process something, not just expose an instance of a class. A property is the more technically correct approach, and it's not complicated.

Alternatively, you can simply expose a public variable and then run code in some other process to create the instance, so . . .
Code:
Private Const FN As String = "frmDetails"

Public GlobalFormDetails As frmDetails

Sub CreateDetailsInstance()
[COLOR="Green"]  'opens the form if it isn't already open[/COLOR]
  if not currentproject.allforms(FN).isloaded then docmd.openform FN
[COLOR="Green"]  'assigns to global variable[/COLOR]
  set GlobalFormDetails = form(FN)
End Sub

. . . but what happens if your form gets closed now? If you do it this way, each consumer has to (should) check if the variable is present. This approach is not simpler and is much less robust.
 
Thanks lagbolt

I think I'm starting to get it now. Property Get doesn't seem all that complex now, appreciate you taking the time to explain it for me!
 

Users who are viewing this thread

Back
Top Bottom