Cancel OnClose event when switching to design view

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kpfunf
    New Member
    • Feb 2008
    • 78

    #1

    Cancel OnClose event when switching to design view

    I have an OnClose function that opens a switchboard (SwitchOpen). This function is placed in numerous objects' (reports and forms) OnClose event, and works great. However, a very annoying occurrence happens: when I have an object open, then click Design View, the OnClose event triggers, causing the switchboard to open event though the object isn't truly "closed". What event should I place the SwitchOpen under so that it only fires when the object is truly closed and not just in design view?
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    In point of fact, you are closing the form when you switch to Design View. Access also considers the form to be UnLoading when this happens, so moving it here won't help either. I'm not sure you can do anything except remark out the line opening the switchboard when you're developing the form, then un-remark it when you're ready to go into production. This is frequently done for situations just like this.

    Linq ;0)>

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      I agree with Linq here I'm afraid. The object is closed when you switch to design view.

      However, you may want to play with the following procedure call to see if you can determine whether or not the item is open at the time your code would otherwise start the switchboard.
      Code:
      Call DoCmd.SelectObject(ObjectType:=acForm, _
                              ObjectName:={YourObjName}, _
                              InDatabaseWindow:=False)

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        You'll need to use On Error code I suspect, and probably inspect the Err object immediately after the call.

        Let us know if this helps and how you get on with it.

        Comment

        • kpfunf
          New Member
          • Feb 2008
          • 78

          #5
          Thanks NeoPa. I'm play aroud with that (as times permits!).

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            No worries. Always a pleasure :)

            Come back if you need more help.

            Comment

            • MrBillBenson
              New Member
              • Oct 2008
              • 2

              #7
              I ran into this EXACT same problem today. One of the reasons I think I used to program in VB and use Access as a Back-end instead of using Access as a programming environment.... Of course, where I am now, I am stuck with Access only. One workaround is to disable the Close button of the form, use your own command button instead. On the Click event of the command button, set a form level control variable = TRUE. Then use the value of that variable to branch in the Unload or Close event according to whether you want to call the switchboard form or not. If the user enters design view (bypasses the command button), the value of the variable will be FALSE ... so this code in the Unload event will achieve what you want:
              Code:
              Private Sub Form_Unload(Cancel As Integer)
              If mbFormControl Then 
                 doCmd.OpenForm "frm_Switchboard"
              End If
              End Sub
              Last edited by NeoPa; Oct 2 '08, 01:42 PM. Reason: Please use the [CODE] tags provided

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                Hi Bill. Welcome to Bytes!

                I would just make a quick suggestion referring to your idea of disabling the close button. Generally it's not necessary.

                If, instead of putting code in the Event Procedure of your exit button, you put the code instead in the Form_Close() Event Procedure, and simply call Me.Close in the Event Procedure of your exit button, then all methods of terminating your form will be fully handled.

                Does that make sense?

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  Hello, gentlemen.

                  It maybe slightly cumbersome, but it is possible to use the following feature:
                  Form_Timer event in a form being opened (unlike Form_Open, Form_Load and Form_Current event) fires after a calling form has been closed or switched into design mode.
                  Form in design view unlike closed one could be referenced via Forms collection and thus distinguished.
                  So, the whole logic may be the following:
                  • In Form_Close event handler SwitchBoard form is opened with form name in OpenArgs.
                  • In SwitchBoard Form_Timer event handler Forms collection is being checked for presence of the form and, if that seems insufficient, Form.CurrentVie w property may be checked as well.


                  Regards,
                  Fish

                  Comment

                  • MrBillBenson
                    New Member
                    • Oct 2008
                    • 2

                    #10
                    Originally posted by NeoPa
                    Hi Bill. Welcome to Bytes!

                    I would just make a quick suggestion referring to your idea of disabling the close button. Generally it's not necessary.

                    If, instead of putting code in the Event Procedure of your exit button, you put the code instead in the Form_Close() Event Procedure, and simply call Me.Close in the Event Procedure of your exit button, then all methods of terminating your form will be fully handled.

                    Does that make sense?
                    Hi NeoPa, it certainly does. I will make the change.

                    Bill

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32669

                      #11
                      Very pleased to hear it Bill :)

                      Needless to say, I never have that problem any more.

                      Comment

                      Working...