Solved

OpenArgs On Form_Load not triggered

Posted on 2004-10-29
453 Views
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?

Thanks
0
Question by:BillPowell
    7 Comments
     
    LVL 58

    Expert Comment

    by:harfang
    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
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    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
     
    LVL 58

    Expert Comment

    by:harfang
    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
     
    LVL 11

    Author Comment

    by:BillPowell
    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
     
    LVL 58

    Accepted Solution

    by:
    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
     
    LVL 58

    Expert Comment

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

    Author Comment

    by:BillPowell
    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

    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