One of my recent adventures in Access was to fix an issue where occasionally a class instance setup by the Form Open event was somehow getting blown away. I’m frankly not sure why. As far as I know the global variables on the form only get reset if you close and reopen the form or if the VBA environment gets reset. I believe the only way that typically can happen is if the user gets into debug mode.
I’m pretty sure my users aren’t getting to debug mode, but somehow the instance gets blown away anyway. This particular class hooks into the Form Activate Event (as well as others) and starts an update process for other subreports on the form making up a dashboard for the users of the application to see at a glance what they want to see in the system in a realtime manner.
I was setting up this variable with the instance of the class only on Form open (actually also on a timer during the initial open because it wouldn’t run when you first opened the app and the library hadn’t been linked yet). The linking of the library kills the instance too.
I had chosen to only load the variable in the Open event of the form because I thought it would remain safe and just keep working. Since I learned this wasn’t the case, I decided to “Lazy Load” the class in the form Activate event.
Lazy loading checks to see if you already have a valid instance loaded, and if not, loads it. Here’s the applicable parts of the code in the Form’s code module:
Private AutoLoadReports As ECI_AutoLoader Private Sub Form_Activate() DoCmd.Maximize If AutoLoadReports Is Nothing Then Set AutoLoadReports = New ECI_AutoLoader AutoLoadReports.Setup Me End If End Sub