Current form? (1 Viewer)

sja13

Professional Idiot
Local time
Today, 01:22
Joined
May 3, 2017
Messages
63
I have a Form which is a main menu form, on which there are buttons which, when clicked, invoke other Forms. These other Forms have a Query as their ControlSource, and this Query is unfiltered, so it gets all of the records in a particular Table.

The "master" form is called (unsurprisingly) "frmMainMenu"
The VBA invoked when the Command Button "cmdManufacturer" is clicked on frmMainMenu follows:-
Code:
Private Sub cmdMMManufacturers_Click()
  DoCmd.OpenForm ("frmManufacturer")
End Sub
In the Form frmManufacturer, the following code exists, which I believe is fired when the Form becomes current
Code:
Private Sub Form_Current()
  Call AtNewRec(Recordset)
End Sub
Within the VBA code AtNewRecord the following code exists
Code:
Public Sub AtNewRec(recSet As Recordset)
Dim frm                     As Form
Dim lngC                    As Long
Dim lngL                    As Long
'*
'** Get addressability to the
'** currently displayed Form, and
'** identify the current record's
'** position and the final record's position.
'*
  Set frm = Screen.ActiveForm
  Select Case UCase(frm.Name)
    Case "FRMBOARD", _
         "FRMITEM", _
         "FRMITEMTYPE", _
         "FRMLOCATION", _
         "FRMMANUFACTURER"
    Case Else
      Exit Sub
  End Select
  With recSet
    lngC = .AbsolutePosition + 1
    lngL = .RecordCount
  End With
Using a Watch, after the cmdManufacturer button on frmMainMenu is clicked I can see that "frm.Name" in the above code is still set to "frmMainMenu", which implies that
Code:
  Set frm = Screen.ActiveForm
isn't doing what I thought it was (i.e. getting details for the Form referenced in the DoCmd.OpenForm instruction).

Can anyone help me by explaining what misconceptions I'm labouring under?
Thanks in advance....
 

MarkK

bit cruncher
Local time
Yesterday, 17:22
Joined
Mar 17, 2004
Messages
8,193
...which I believe is fired when the Form becomes current
Code:
Private Sub Form_Current()
  Call AtNewRec(Recordset)
End Sub
No. The current event fires every time the form loads a record, or arrives at a new record.

I would not rely on Screen.ActiveForm. You are passing the form's recordset to this Sub, why not just pass the form itself, like...
Code:
Public Sub AtNewRec(frm as Access.Form)
   Dim lngC                    As Long
   Dim lngL                    As Long

   With [COLOR="Blue"]frm.Recordset[/COLOR]
      lngC = .AbsolutePosition + 1
      lngL = .RecordCount
   End With
...and the call to this sub becomes...
Code:
Private Sub Form_Current()
  Call AtNewRec(Me)
End Sub
This way we don't need the Select Case either because only qualifying forms will pass themselves in to the routine...

So this is simpler, and much more reliable. Screen.ActiveForm never returns a subform, and in some cases there is no active form, or it's not the form you expect.

hth
Mark
 

sja13

Professional Idiot
Local time
Today, 01:22
Joined
May 3, 2017
Messages
63
Thanks for the kind and useful explanation. I'll amend my code (and thinking!) accordingly....
 

sja13

Professional Idiot
Local time
Today, 01:22
Joined
May 3, 2017
Messages
63
Works a treat - less code, easier to follow, and more elegant, because I'm passing the "Object of interest" not a subset of that Object.
Thanks MarkK!
 

Users who are viewing this thread

Top Bottom