Module variable (1 Viewer)

ClaraBarton

Registered User.
Local time
Yesterday, 17:50
Joined
Oct 14, 2019
Messages
654
I have a module with a simple routine to position the form to the last record
Code:
Public Sub RepositionForm(frm As Form)
10    On Error GoTo RepositionForm_Error
      Dim strWhere As String

30            strWhere = "LocID = " & lngLoc
70        frm.Requery
80                With frm.RecordsetClone
90                    .FindFirst strWhere
100               If Not .NoMatch Then
110                   frm.Bookmark = .Bookmark
120               End If
130           End With
         
140       frm![fldItem].SetFocus
150       If Len(frm.fldItem & "") = 0 Then
160           Exit Sub
170       Else: frm.fldItem.SelStart = 0
180           frm.fldItem.SelLength = Len(frm.fldItem)
190       End If
When I call it from the form module it's:
Code:
RepositionForm me
But I have a couple routines in the same module that use it also. How do I find the form name?
Would the frm variable carry through? Would you set it at the module level as Private?
There is surely a simple, proper way that I'm missing.
 
Last edited:
But I have a couple routines in the same module that use it also.
What exactly does that mean?
If you are calling this procedure
RepositionForm me
you could simply call the other procedures
Procedure2 Me
Procedure3 Me.

If you are always doing the same three Procedures then make a single wrapper procedure

Code:
Public Sub RepositionAndMore (frm as Form)
  RepositionForm Frm
  Procedure2 Frm
  Procedure3 Frm
end sub
 
If it was something more complicated than that where you have lots of methods that need to use lots of common properties then maybe make a class.

Dim FM as new FormManipulator
FM.initializeclass me, someproperty, someotherprop, prop2, prop3, prop4.)
FM.Reposition
FM.Method2
FM.method3

Now reposition and all methods can be using the same form and all the same properties without having to pass more than once.
 
OK... over my head here! RepositionForm is called from the form, hence... RepositionForm me.
But I also need to call RepositionForm from within the module where the RepositionForm sub is located. Passing the me name through more than one sub? How does that work?
RepositionForm me works great.
But what if I call JumpSomeHoop and JumpSomeHoop (which doesn't require the form name) calls RepositionForm. That's where I don't know how to get the form name
 
I still do not completely understand. But lets say sometimes you call jumpsomehoop and it does something, but when you pass it a form it will then call repositionform

Code:
Public Sub JumpSomeHoop (Optional frm as access Form = nothing)
  code for jump some hoop
  'jump some hoop does not always call reposition form unless you pass it a form
  'it does stuff without alwasy calling repositionform
  if not frm is nothing then
    repositionForm frm
  end if
end sub

now you can call
JumpSomeHoop
or
JumpSomeHoop Me
 
Why would jumpsomehoop call repositionform if it did not require the form? :(
You need to break your processes down even more.
 
The procedures in the same module need to pass a Form variable (not the form's name) when calling RepositionForm(), just as when calling it from a form's module. No telling what those procedures do, with what, or from where they are called but somehow, they need to gin up a pointer/object variable referencing the Form instance in question before making the RepositionForm() call.

There can be a module-level Form variable, as noted, although it must be set somehow.

Two other approaches also exist. The calling procedures can create a Form object reference if they can't get the necessary Form reference otherwise. The most obvious way of doing this is probably using DoCmd.OpenForm, or iterating Forms or AllForms. One also can call a function (perhaps named GetForm()) that returns the required form reference given the proper arguments.

Alternatively, those procedures can be designed (if they aren't already) to take a Form reference as an argument from their calling procedures. This is where using ByRef (and doing so expressly) is quite handy, not to mention efficient. Doing this, one can pass the same object reference to, and through, any number of similarly configured procedures. So, if you swim up the call stack from the routines in the same module and find that their calling procedures have or can get the necessary Form reference, this would be the best approach. I do this extensively in my library, passing or returning any number of forms, controls, native and bespoke collections, and DAO objects, and it all runs like lightning.
 
Last edited:
But what if I call JumpSomeHoop and JumpSomeHoop (which doesn't require the form name) calls RepositionForm. That's where I don't know how to get the form name
If a lower level procedure requires the form reference, then you must pass it as a parameter
 

Users who are viewing this thread

Back
Top Bottom