On Load runs when closing form (1 Viewer)

spikepl

Eledittingent Beliped
Local time
Today, 03:59
Joined
Nov 3, 2010
Messages
6,142
I have an A2007 application running on XP. From main form, Form1, another form, Form2, is opened.

When I attempt to close the application by clicking in the cross in the rh-corner of Access window, I get a crash midway through the OnLoad of Form2. I cannot figure out why the heck the On Load event fires when the form is being closed, and have some difficulties stepping through the code.

I recall having heard of OnLoad firing when trying to close a Form.

Have you? And do you have any details? (I do not expect you to solve my problem for me from the sparse info I provided - just a hint would be nice)
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:59
Joined
Aug 30, 2003
Messages
36,124
I had that problem with one app. Never figured out why it fired the load event. I got around it by trapping for the error (in my case it was looking for the login form, which it had already closed while quitting.
 

missinglinq

AWF VIP
Local time
Yesterday, 21:59
Joined
Jun 20, 2003
Messages
6,423
I remember Paul having the problem, and I've seen it reported one other time, but have never seen a solution!

Have you tried Compiling the app or running a Compact & Repair? Be sure to create a backup before doing the latter; paradoxically, C & Rs can actually cause corruption!

Linq ;0)>
 

spikepl

Eledittingent Beliped
Local time
Today, 03:59
Joined
Nov 3, 2010
Messages
6,142
I have solved it. Meaning - I made changes so the error no longer appears, but why ??? :D

This is my set up, roughly:

I have a main form Main. On the form there is a QUIT button. It closes the form, and opens an information form, where code backs up the current backed and another db. The backup works only if no tables are connected to the backend (it checks for laddb file). This works only of all the forms are closed.

I have made it so that when you attempt to close Access by clicking on the cross, it says "Use the quit BUTTON". The same applies when attempting to close the form Main by clicking on the cross.

I have a form Form1, that can be opened from Main. When this form is open, I would get the error when clicking on the Access-window's cross. The backup would fail.

I found out that when I have Main open and then Form1, and click the close of the Access window, Access closes first Main and then Form1. My backup fails , and the code goes haywire.

I now prevent unloading of the form Main unless QUIT button was clicked. When that button is clicked I run though AllForms collection and close all open forms except for Main. I then close Main and then call my backup subroutine and form, and everything works just fine.

Why OnLoad of Form1 fired has remained a mystery, but it does not happen any more. One of life's conundrums ... :D
 

Steve R.

Retired
Local time
Yesterday, 21:59
Joined
Jul 5, 2006
Messages
4,675
I had the very same problem with the "on load" event being triggered when the QUIT button was pressed on a form in MS Access 2007. Took me forever to figure that one out since the error message just flashed by as the database closed. The solution, in my case, was to move some code from the form "Close" event to the form "Deactivate" event.

When I attempt to close the application by clicking in the cross in the rh-corner of Access window, I get a crash midway through the OnLoad of Form2. I cannot figure out why the heck the On Load event fires when the form is being closed, and have some difficulties stepping through the code.
I may have an alternate solution for you. I relocated the following code below from the "Close" event to the "Deactivate" event. This allows a form to close silently, even when there is an error. It seems that when an error occurs with the "Close" event MS Access has no where to go, since the "Close" event is the last step.
Code:
Public Sub Form_Deactivate()
    On Error GoTo ERR_Form_Deactivate
    If CurrentProject.AllForms(csSearchForm).IsLoaded Then Forms(csSearchForm).SetFocus Else DoCmd.OpenForm csSearchForm
    Exit Sub
ERR_Form_Deactivate:
    Resume Next
End Sub

I now prevent unloading of the form Main unless QUIT button was clicked. When that button is clicked I run though AllForms collection and close all open forms except for Main. ...
With the program I wrote, the user has to return to the main form before they can exit. However, one form still remains open in the background. When it comes time for a future revision, I will look into modifying the QUIT button to implement your suggestion.
 
Last edited:

DemonDNF

Registered User.
Local time
Yesterday, 21:59
Joined
Jan 31, 2014
Messages
77
I have a similar problem in Access 2002 Win XP, so I placed checkpoints at the first line of every SUB in fmrMain.

- Form_Load does not execute when I start up frmMain.
- FrmMain and the 2 nested subforms display normally, data and all.
- Form_Load does execute when I click the EXIT button.


I also thought of corruption, so I:

- deleted all the relationships.
- copied the structure of every table to Xtablename.
- deleted the old tables.
- renamed the new tables.
- reset all the relationships.

Same thing, Form_Load does not trigger when frmMain is first opened so none of the setup VBA code is run.

I checked the Events in frmMain properties; On Load, On Unload and On Current all have an Event Procedure listed.

I lost trust in this database so I'm started all over again; testing each process as I go. I could patch like the guys above and force a square peg in a round hole, but I can't distribute something I don't trust.

I love Access but this sucks royally.

Robert
 

DemonDNF

Registered User.
Local time
Yesterday, 21:59
Joined
Jan 31, 2014
Messages
77
I did a test to see if there was a bug in my version of Access 2002. I figured I'd start at the beginning and eliminate the easy stuff. Created a new database with table and form.

I opened the form and my msgboxes in Open, Load and Current came up before the cursor showed up on the form. So now I know somewhere along the way I screwed up the coding and the link to initiate Load got messed up.

I also tested creating an MDE and same thing; I can create one using my new table and form so I know there's nothing missing. I must have errors in my code that prevents me from making one.

I've went through the 10 commandments and read up more on error handling so I don't mind starting from scratch.

Robert


EDIT: There's several causes for a corrupted MDB:
http://allenbrowne.com/ser-25.html

In my case, the most likely causes are modifying VBA code as I debug, not closing things I open and the several Access lockups and ensueing messed up record pointers.


SEARCH STRINGS: form does not load, form loads on quit close, cannot make mde, corrupt database
 
Last edited:

Krysova

New member
Local time
Yesterday, 18:59
Joined
Jul 8, 2017
Messages
2
I know this is an old thread, and solved, but I wanted to share my solution. The info in this thread just helped me figure this problem. I recently added a hidden form to maintain persistent connections to the back-end databases. The main switchboard form, in its Open event, opens that hidden form. That hidden form apparently caused the main form's Load event to fire when closing it (the main form). I simply added a line to the main form's Unload event that closes the hidden form. Et Voila, the Load event no longer fires inappropriately.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:59
Joined
Feb 28, 2001
Messages
27,148
I've never seen this myself, but I'm thinking that it might be caused by something that occurs in the offending form's OnOpen event in such a way as to defer the OnLoad event execution. Damned if I can figure what it would be since I've never seen it. Maybe a DoEvents or something else that attempts to yield control in the OnOpen event?

If any of you have code that you think might trigger this situation, I might put in an event log that, when you use an event routine, it logs the event entry and the event exit. That would tell you if you called the OnLoad twice or only called it once but in deferred fashion.
 

prley

Been Around the Block
Local time
Yesterday, 21:59
Joined
Oct 26, 2007
Messages
37
I know this is an old thread, and solved, but I wanted to share my solution. The info in this thread just helped me figure this problem. I recently added a hidden form to maintain persistent connections to the back-end databases. The main switchboard form, in its Open event, opens that hidden form. That hidden form apparently caused the main form's Load event to fire when closing it (the main form). I simply added a line to the main form's Unload event that closes the hidden form. Et Voila, the Load event no longer fires inappropriately.

I too was experiencing this on a shutdown form I started using to force users to shutdown. It was hidden form. I added code to the unload event of the main form to see if this form was still loaded and if so to issue a docmd.close to the open form. Now It's not firing the load event. Go figure.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Yesterday, 21:59
Joined
Oct 17, 2012
Messages
3,276
Time for a little necromancy.

I just encountered this very situation.

My app has a background form (used because in A2016, the application background is white and cannot be changed, and some of my users get migraines from it). The form resizes itself when it is loaded to cover the entire background, and then if you click the form, it takes any visible, open objects other than itself and moves them to the front. Here is the entire code:

Code:
[COLOR=green]'*********************************************************************************
'** MODULE:         frmBackground
'** CreatedBy:      Scott Prince
'** CreationDate:   5/1/2018
'** Description:    Provides application background in place of Access White.
'*********************************************************************************[/COLOR]
 
Option Compare Database
Option Explicit

Private Sub Form_Activate()
    
    [COLOR=green]'Show all OTHER forms that are flagged as visible.[/COLOR]
    Call ShowLoadedScreens
    
End Sub
 
Private Sub Form_Load()
    
    [COLOR=green]'Resize the form.[/COLOR]
    Call SetBackground
    
End Sub
 
Private Sub SetBackground()
[COLOR=green]'*************************************************
'Version:               1.0.0
'Created By:            Scott L Prince
'Date Created:          5/1/2018
'Purpose:               Resizes frmBackground to precisely fit the application window.
'Parameters:            None
'Returns:               N/A
'Dependencies:          None
'Comments:              None
'*************************************************[/COLOR]
Dim WH As Long
Dim WL As Long
Dim WT As Long
Dim WW As Long

    With Me
        DoCmd.Maximize
        WT = .WindowTop
        WL = .WindowLeft
        WH = .WindowHeight
        WW = .WindowWidth
        DoCmd.Restore
        Call .Move(WL, WT, WW, WH)
    End With
End Sub
 
Private Sub ShowLoadedScreens()
[COLOR=green]'*************************************************
'Version:               1.00.00
'Created By:            Scott L Prince
'Date Created:          5/1/2018
'Purpose:               Displays all OTHER forms flagged as visible.
'Parameters:            None
'Returns:               N/A
'Dependencies:          None
'Comments:              None
'*************************************************[/COLOR]
[COLOR=green]'Version:               1.00.01
'Edited By:             Scott L Prince
'Edit Date:             7/6/2018
'Description:           Added code to show opened reports and queries.
'*************************************************[/COLOR]
Dim frm As Form
Dim rpt As Report
Dim obj As AccessObject
 
    [COLOR=green]'Show all loaded forms that are not this form.[/COLOR]
    For Each frm In Application.Forms
        If frm.Visible And Not (frm.Name = Me.Name) Then frm.SetFocus
    Next frm
    
    [COLOR=green]'Show all loaded reports.[/COLOR]
    For Each rpt In Application.Reports
        DoCmd.SelectObject acReport, rpt.Name
    Next rpt
    
    [COLOR=green]'Show all open queries.[/COLOR]
    For Each obj In Application.CurrentData.AllQueries
        If obj.IsLoaded Then DoCmd.SelectObject acQuery, obj.Name
    Next obj
    
ProcExit:
    If Not obj Is Nothing Then Set obj = Nothing
    If Not rpt Is Nothing Then Set rpt = Nothing
    If Not frm Is Nothing Then Set frm = Nothing
    Exit Sub
 
ErrHandler:
    Trap.Handle Me.Name, "Form_Load"
    Resume ProcExit
 
End Sub
There is also a main menu ('Switchboard') open, with a fairly involved Form_Load event.

The issue was that if the background form is open and the exit applicatioin button is run (whose only code is 'DoCmd.Quit'), an error message pops up complaining about an object variable not being set (probably the error handler class in the Load event), then the app closes.

If the background form is NOT loaded, then no error occurs.

My solution was just to move the entire Form_Load event to Form_Open, which solved the problem. Testing showed that closing frmBackground first would fix it as well.
 

imohamed

New member
Local time
Yesterday, 18:59
Joined
Aug 9, 2019
Messages
1
I am experiencing the same issue. I have no hidden or background forms. The only way to exit my application is by closing all of access. Whenever the exit occurs, the form_load event is triggered on various open forms.

Embarrassingly, this issue has existed for me since Access 2010. I upgrade every version hoping the issue will just disappear. For many years I just asked my users to deal with the errors on exit, it was jarring but did not hurt anything. Today I finally sat down and decided to dig into the issue. Took forever to figure this out as many of you know it is impossible to break on the error, whenever I stepped through the code, it would not happen. Whenever the error occurred and I went to debug, the code window would popup quickly and then close. I finally decided to record my screen as it happened and stepped through the video frame by frame. That's how I figured out that form_load was being triggered. That's also how I stumbled upon this thread.

I haven't done a lot of searching but I haven't found much else on this issue. I solved it by creating a form level variable IsLoaded. When the load event is triggered I check that variable and set it to true if it is false, exit if it is true.

This is absurd though, why the heck is Form_Load being triggered on exit??
 

Users who are viewing this thread

Top Bottom