OpenArgs On Form_Load not triggered

Posted on 2004-10-29
Last Modified: 2008-02-01
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?

Question by:BillPowell
    LVL 58

    Expert Comment

    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... :)
    LVL 44

    Expert Comment

    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')

    LVL 58

    Expert Comment

    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.

    LVL 11

    Author Comment

    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.
    LVL 58

    Accepted Solution

    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"
            End With
            DoCmd.OpenForm "FormA", OpenArgs:="yyyyy"

    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"

    Well, there you go!
    LVL 58

    Expert Comment

    Sorry, in the last line I meant:
       Forms!FormA.JumpToRecord Me.strMyKey
    although the bracketed version should have worked in that particular case...
    LVL 11

    Author Comment

    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.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now