OpenArgs On Form_Load not triggered

I have two forms which I navigate back and forth between then viewing associated records.  I use docmd.openform with the openargs being the ID of the record I want to goto.  In the load event for each form I use this code to get to the record I need:

If Not IsNull(Me.OpenArgs) Then

  Set rs = Me.RecordsetClone
  rs.FindFirst "SDVN_NUM = '" & Me.OpenArgs & "'"
      If Not rs.NoMatch Then
      Me.Bookmark = rs.Bookmark
    End If
   
End If

This works great the first time, however when both forms are opened the load event is not triggered and it does not perform navigation.  I would like to solve this without having to close the forms in between calls. Just wondering how others handle this type of situation?

Thanks
LVL 11
BillPowellAsked:
Who is Participating?
 
harfangConnect With a Mentor Commented:
Well, ok, if you want the purist's version...

Declare a public string in FormA, as in:

    Public ActivateArgs as String

Use any of the "FormIsOpen()" functions (there is one in Access Help, btw), and then go...

    If FormIsOpen("FormA") Then
        With Forms!FormA
            !ActivateArgs = "xxxxx"
            .SetFocus
        End With
    Else
        DoCmd.OpenForm "FormA", OpenArgs:="yyyyy"
    EndIf

Now you need to check Me.OpenArgs in Form_Open *and* Me.ActivateArgs in Form_Activate....
It it really better?

Now the really, really, really object oriented approach...
(provided that your form isn't a dialog box, which seems not to be the case)
Create a public *method*!!! In FormA:

Public Sub JumpToRecord(pstrKey)
   ' do your stuff here
End Sub

Then you can...

    DoCmd.OpenForm "FormA"
    Forms!FormA.JumpToRecord(Me.strMyKey)


Well, there you go!
0
 
harfangCommented:
The event you want is Form_Activate

Create a normal module, say basGlobal, containing:

    Global gstrArgsForFormA As String

Whenever you need the form A, use:

    gstrArgsFormFormA = "cboID;123"   ' or whatever you would feed as OpenArg
    Docmd.OpenForm "FormA"

In Form A, use:

Private Sub Form_Activate()

    If gstrArgsForFormA <> "" Then
        ' use the argument here
        gstrArgsForFormA = ""   ' (reset)
    End If

End Sub


This works, I use it all over the place... :)
0
 
Arthur_WoodCommented:
if you are not closing and disposing  of the form (Set Form = Nothing) then the form, once loaded, stays in memory, and Form_Load is NOT called again.  The form is displayed (.Visible = True), but NOT re-loaded.  That is why Form_Load is the wrong place.  When .Visible = True occurs, the Form_AcTivate is then called (the form has been 're-activated')

AW
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
harfangCommented:
Not quite, AW

Form_Activate is the "On Got Focus" of forms. It has little to do with .Visible (an unvisible form can't get the focus).

Also "set Form = Nothing" has no clear meaning in this context. It's more confusing than helpful, imho.

Cheers
0
 
BillPowellAuthor Commented:
Good suggestion harfang.  My only workarounds previously was to ensure I closed the calling form so that the onload would get triggered again when I needed that form again.  I have used a modified version of your method in the past, but had always thought of it as less than ideal solution because of having to resort to using a global variable.  Now that I see others use it, I guess it cant be that bad.
0
 
harfangCommented:
Sorry, in the last line I meant:
   Forms!FormA.JumpToRecord Me.strMyKey
although the bracketed version should have worked in that particular case...
0
 
BillPowellAuthor Commented:
Thanks for the extra tidbit.  Im thinking Ill stick to the global variable option.  I might add a custom property to write which form was the calling form.  That way I can opt out of navigating to the record if the form was not called by the last form that modified the global variable.

Regards,

Bill
0
All Courses

From novice to tech pro — start learning today.