crazyTech
crazyTech

Reputation: 1477

MS Access A certain Form's Form_Load() Only invoked first time that the user views said forms, but fails to be invoked in future viewings

I have very a rudimentary understanding of Microsoft Access and VBA Code. On my work desktop, I have Microsoft Office Professional Plus 2013 Access I've been tasked to create a MS Access application with an Access DB.

In the Visual Basic files, I have pseudo-code behind files for the various MS Access forms.

Most of my MS Access forms files have a subroutine like the following one:

 Private Sub Form_Load()

     Dim db As DAO.Database
     Dim qdfSelectQuery As DAO.queryDef
     Dim argumentsStringArray() As String

  If Len(Me.OpenArgs) > 0 Then

     argumentsStringArray = Split(Me.OpenArgs, "|")

    …...Blah blah initialize some variables using the argumentsStringArray Blah blah …..;
  End If

 End Sub

Now the "Navigation" within the application can move back and forth between different forms, and it could go back to an earlier form that was already seen/used by the user of the application.

A Particular Form's Form_Load() subroutine does get executed the very first time that said form runs within the context of a particular execution run of said MS Access Application. However, if the application navigates back to a particular form that the user has already seen then it will Not execute said Form_Load() subroutine.

Could someone please tell me what code modifications that I would have to make in order to ensure that a Particular Form's Form_Load() subroutine executes regardless of how many times the user has seen it?

Upvotes: 0

Views: 1515

Answers (2)

Erik A
Erik A

Reputation: 32642

My solution would be the following:

Instead of:

Private Sub Form_Load()
     Dim db As DAO.Database
     Dim qdfSelectQuery As DAO.queryDef
     Dim argumentsStringArray() As String

  If Len(Me.OpenArgs) > 0 Then

     argumentsStringArray = Split(Me.OpenArgs, "|")

    …...Blah blah initialize some variables using the argumentsStringArray Blah blah …..;
  End If
 End Sub

You use:

Public Sub InitializeForm(SomeArgs As String)
     Dim db As DAO.Database
     Dim qdfSelectQuery As DAO.queryDef
     Dim argumentsStringArray() As String
  If Len(SomeArgs) > 0 Then
     argumentsStringArray = Split(SomeArgs , "|")
    …...Blah blah initialize some variables using the argumentsStringArray Blah blah …..;
  End If
 End Sub

And instead of

DoCmd.OpenForm "MyForm", OpenArgs := "Something"

You do

DoCmd.OpenForm "MyForm"
Forms!MyForm.InitializeForm "Something"

This allows way more flexibility than relying on OpenArgs, e.g. you can pass objects and strongly-typed values, return values, etc.

Upvotes: 2

Mathieu Guindon
Mathieu Guindon

Reputation: 71187

The Load handler runs once in the lifetime of the object.

If you need it to run multiple times, don't reuse the same object. DoCmd.OpenForm works off the form's default instance, so unless you unload or otherwise destroy the instance, it remains loaded, and next time it's displayed it still holds whatever instance state it had when it was last dismissed.

If an Access form supports unloading (not familiar with Access, and can't test ATM), then Unload MyForm should unload it, making the Load event fire again the next time the form is referenced.

If you can New up a form and Show it like you could with a UserForm, do it - forms are objects in their own rights, and this global default instance stuff is hiding the object nature of it... making basic assumptions fail (like, a Load handler being expected to run every time the form is shown).

Alternatively, move your Load code into a new handler for the Open event, which would be invoked whenever the form is open, not merely loaded.

Upvotes: 2

Related Questions