Results 1 to 8 of 8
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111

    OnClose event is triggered when opening form in laoyout mode. Why?

    Please explain why this scenario is occurring as I am baffled.

    Below is the code I have on for the Data Entry Form OnClose Event on my form. I want to check to see if another form is open, and if not open it and then make it Visible. If the form is in layout view or design view then I don't want to automatically open the Main Menu Form. (Data Entry person should always have the Main Menu form open. Its visibility changes as the uses moves from one form to another.)
    All forms are opened from the Navigation page for testing purposes.

    Code:
    Private Sub Form_Close()
    Dim varTest As Integer
    varTest = Me.Form.CurrentView
    Debug.Print varTest
        If Me.Form.CurrentView = 1 Then
            Debug.Print "Form mode"
            If CurrentProject.AllForms("mnuMainMenu").IsLoaded = False Then
                Debug.Print "Main Form Closed"
                DoCmd.OpenForm ("mnuMainMenu")
            End If
        Else
            Debug.Print "Layout mode"
            Exit Sub
        End If
        Forms!mnuMainMenu.Visible = True
    Scenario 1 (results as expected)
    Both Main Menu Form and Data Entry Form are open in Form view. Intermediate window displays the following:
    Code:
     1 
    Form mode
    Scenario 2 (results as expected)


    Main Menu Form is Closed and Data Entry Form is open in Form view. Intermediate window displays the following:
    Code:
     1 
    Form mode
    Main Form Closed
    Scenario 3
    Open and close Data Entry Form in Design view has no results in intermediate window regardless of whether Main Menu is open. (As expected)

    Scenario 4

    Main Menu Form is Closed and Data Entry Form is closed in layout view but was originally opened in Design view. Intermediate window displays the following when I close the form (As expected)
    Code:
     7 
    Layout mode
    Now here is where it confuses me.
    Scenario 5
    Main Menu Form is Closed and I open the Data Entry Form in Layout view, this pops up in the intermediate window:
    Code:
     1 
    Form mode
    Main Form Closed
    And the Main Menu Form is opened.

    When I close the Data Entry Form in layout view I get the following (As expected)
    Code:
     7 
    Layout mode
    I think it is odd that when you open a form in layout view it triggers the OnClose event of the form. What is really going on in this instance?

  2. #2
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I don't get #5. If I open a form in layout view but from the nav pane, then close it, of course the close event runs. But you're saying I open the Data Entry Form in Layout view (but not how or from what state it's being opened) and suggesting that code is running when the form is opened in that view. That would suggest you have form open code that runs only when layout is the view. I can't replicate the behavior you're experiencing, so maybe we'd need to see the code behind the entry form.

    If I open as
    design > form > close, close event runs
    design > layout > close, close event runs
    design > close, close event doesn't run
    layout > form or form > layout, close event doesn't run (note, that means switching from form to layout or vice-versa isn't considered a form close)
    layout > close or form > close, close event runs
    That's what I would expect.
    Last edited by Micron; 05-12-2017 at 04:41 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111
    I don't get #5 either. When I open the form from the navigation pane in layout view, the intermediate window displays the the debug.print information as described above right after I OPEN IT. I see this code
    Code:
     1 
    Form mode
    Main Form Closed
    BEFORE I close the form.

    Quote Originally Posted by Micron View Post
    I don't get #5. If I open a form in layout view but from the nav pane, then close it, of course the close event runs. But you're saying I open the Data Entry Form in Layout view (but not how or from what state it's being opened) and suggesting that code is running when the form is opened in that view. That would suggest you have form open code that runs only when layout is the view. I can't replicate the behavior you're experiencing, so maybe we'd need to see the code behind the entry form.

    If I open as
    design > form > close, close event runs
    design > layout > close, close event runs
    design > close, close event doesn't run
    layout > form or form > layout, close event doesn't run (note, that means switching from form to layout or vice-versa isn't considered a form close)
    layout > close or form > close, close event runs
    That's what I would expect.

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    then you must have event code for open or load events.
    so maybe we'd need to see the code behind the entry form.
    In absence of the aforementioned, I don't see what else I have to offer. Sorry.

  5. #5
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111

    OnClose event is triggered when opening form in layout mode

    I am not sure I started a new thread or not. This was the only way I could figure out how to attach database demonstrating my issue.

    In reference to:

    https://www.accessforums.net/showthread.php?t=66083

    I have attached stripped down database to show you what is occurring.

    I do not see what it is that is causing this issue.
    Attached Files Attached Files

  6. #6
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I should have suggested you put breaks on all the pertinent events so you could follow them. When opening as you state in scenario 5 this is what I see:
    opening entry form in layout view from nav pane:
    the open event runs
    the load event runs (I don't put non-data related property settings in the load event)
    the close event runs where it says, 'if main menu is not loaded, then open it'
    (this is likely where it switches from design view to layout. I've never checked, but perhaps this is the usual behaviour)
    main menu open event runs
    at setfocus line, execution branches to load event, completes it & returns to open event
    data entry form open event runs again (probably as a result of switching to another view at this point)

    If you interrupt the process by stopping the code you'll get a system message about having to close the window as the view switch cannot be completed. I don't recall at what point I did this, but you can play with it following the above and likely figure out which view it was going to based on your immediate window.

    So unless you expect users to be opening forms in layout view, you shouldn't have an issue. Otherwise, I guess you have to tweak the process.

  7. #7
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    111
    I do not expect this to be a real issue. I just thought it odd and wanted to know what was different about using Layout view than Form and Design view. Coding is easier when you understand the inner workings and this one had me baffled. Thanks for helping.

  8. #8
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Seems like this has to do with the order of events. Looks like the form actually closes and reopens when being opened from the nav pane in layout view, which surprises me. I think when I wrote this is likely where it switches from design view to layout I think I meant switches from normal to layout. Your open & load code wouldn't have run if opened in design view. You might want to check this out
    https://support.office.com/en-us/art...7-CE86553682F9
    so you have some idea of the order of events. It will come in handy, but I see nothing there about what happens when you open a form in design view. If your code is an issue, you'll have to figure out a tweak that prevents it. One way is to pass a value in the form's OpenArgs when the form is opened by user action from another form. If the property has no value when the form opens, bypass the code and maybe close it or disable controls instead.

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

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2015, 10:25 AM
  2. OnClose event broke my database. Please help!
    By alexandervj in forum Access
    Replies: 2
    Last Post: 12-04-2013, 04:11 PM
  3. OnClose event and exit/quit buttons
    By atran in forum Access
    Replies: 1
    Last Post: 07-24-2012, 04:14 PM
  4. onclose event
    By imintrouble in forum Forms
    Replies: 14
    Last Post: 10-18-2011, 08:10 PM
  5. Replies: 7
    Last Post: 03-15-2011, 11:14 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