Results 1 to 15 of 15
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280

    Closing Access with a hidden form

    I have a form in my database that is always open in the background and I either make it visible or hidden as required (this prevents unsightly screen flashes of opening and closing the form). In the Form_Unload of the form I have the following code :-
    If Not gblnExit Then
    Me.Visible = False
    Cancel = True
    End If

    gblnExit is set true when I close the Main form.



    Normally, when I close down Access using top right X button it works OK. It must be closing the Main form which sets the gblnExit True before closing the hidden form. Sometimes this does not work and Access won't close unless I explicitly close the Main form.

    I am wondering if there is an event I can access that is called when Access is closed in which I can set gblnExit True, or if there is a way to force Access to close the Main form before trying to close the hidden form.

    Can't find any information on the internet to say what order Access closes open forms.

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Just add code to the main form's close event to close the hidden form after setting gblnExit.

  3. #3
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Add code to each forms ( a module / function would be best to save lots of typing) close event that checks if your other form is loaded and set the flag appropriately.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Thanks davegri, but I already have that code - that's why it works 95% of the time. It's the rare occasion when it doesn't that I want to fix. I think that in these situations, Access tries to close the hidden form first before the main form, when gblnExit is not set and this causes the whole Access exit event to be cancelled. Incidentally, the Main form is always loaded first in the autoexec macro.

  5. #5
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Thanks Minty, that might help sometimes, but mostly when I close Access there are only two forms open - the Main form and the hidden form. The Main form already sets gblnExit True - see reply to davegri.

  6. #6
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    That's sort of what I was angling towards, check if the hidden form is loaded on all other forms unload events.
    If it's not then open it hidden. By definition it then would be the last form closed?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Yes, but I already do that. The problem seems to be when it tries to unload the hidden form first. When the hidden form's unload event is called it is cancelled and made hidden instead. If access then tried to close the Main form, the Main form's unload event does set gblnExit True and then closes the hidden form itself. So I can only assume that in this case Access is not even trying to close the main form, otherwise it would work.

  8. #8
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You could check on the final form if any other forms are still open and close them first?
    It does sound a bit catch 22 ish
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Incidentally, my Main form's close event is something like this :-
    Private Sub Form_Unload(Cancel As Integer)
    ' Close the other forms.
    gblnExit = True
    DoCmd.Close acForm, "Form1"
    DoCmd.Close acForm, "Form2"
    DoCmd.Close acForm, "Form3" this is the hidden form
    End Sub

    I do not bother to check if the forms are still open. Does this matter, or does Access do something silly like reopen them to then close them? If so, I will have to put in extra code test test if each form is open.

  10. #10
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Thanks Minty, but you seem to be working on the assumption that when you close access, it loops round all open forms and tries to close each one in turn. I am positing that that is not the case - I may be wrong in which case I will have to look elsewhere for the problem.
    Basically, I usually only have two forms open Main and Hidden. If Access closes in the order Main, Hidden the Main form's unload event will have set gblnExit True and called DoCmd.Close acForm, "Hidden" itself. This works OK. If Access closes the Hidden form first, the Close event is cancelled - which seems to cancel the whole exit process.

  11. #11
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    You could try and write the form name as it closes to a local table with a system timestamp, that might prove the closing event timing ?

    Interesting issue.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #12
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Yes, nice idea and might try that when I get time. However it is one of those annoying problems that only happen once in a while and it has not happened today. I was hoping someone might know if my assumption is correct that if one closing event is cancelled the whole Access close event is cancelled. Can't think of any other explanation.

  13. #13
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You could consider removing the application close button so that your own button is the only way to close the db without resorting to pulling the plug or invoking Task Manager. The button code could log somebody out, in which case you can see who logs in but sometimes/always doesn't seem to log out.

    My guess would have been that forms are closed in the reverse order to which they're opened when using the application close button. However, it could be that the speed at which things occur can be such that one time, a form is closed by Access before the code in another one can complete, and that code is related to some form that's already closed. As a general rule, I would always open such hidden forms first, then a main form.

    Apparently you can create a class module and tie the application object to it, although I'm not sure if the list of application methods or properties includes one that will allow you to prevent the application from closing. I supposed you could also iterate over the AllForms collection to see if any are open and decide what to do based on that. What you haven't said is what you're trying to achieve with your current setup. Maybe you don't really need to control the order of closing forms at all.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    Thanks to all contributors to this thread.

    My only reason for this issue was because I have a form that I was constantly opening and closing on my other forms (a further detail form). I did not like the unsightly screen flashes and it looks much better if I make the form hidden when I click the form's close button. If I was still a professional programmer (of 30 years standing), I would not have bothered at all as I don't think most users would even notice or care. However, this Access application is for my own use and hobby and I like things to look nice.

    So, as it stands, I will leave things as they are and live with the fact that maybe once in a while Access won't close down and I then I will have to manually close the main form before finally closing down Access.

    Would have been nice to have a simple solution, but I am not going to go to great lengths to solve what is not really a problem.

  15. #15
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    280
    One final note on this thread which may be of use to someone later. I put a simple msgbox in the unload form event of each form in my database and then closed down Access. This showed the order in which access is closing the forms. It always seems to be in the order as defined by For Each form In Application.Forms
    Since my Main form is always loaded first, this is the first form in the list and hence unloaded first - so no problem.
    I noticed when my hidden form was the first in the list, Access tried to unload this first (the unload event is cancelled - see earlier posts) and then the whole Access close event is cancelled without Access trying to close any other forms.
    It is a mystery how the hidden form could ever be first in the list since closing the Main form always closes the hidden form. Maybe it is as a result of me doing things in design mode and then carrying on running the application.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-24-2018, 11:39 PM
  2. closing forms if they are currently hidden
    By jdashm in forum Access
    Replies: 2
    Last Post: 04-11-2016, 02:32 PM
  3. Replies: 3
    Last Post: 11-24-2014, 11:09 AM
  4. Replies: 0
    Last Post: 01-18-2011, 07:09 AM
  5. Replies: 9
    Last Post: 12-28-2009, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums