• Close All Open Forms (Access 2000 /2002)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Close All Open Forms (Access 2000 /2002)

    Author
    Topic
    #365650

    I am looking for a way to close all open forms through VBA. I have an application that contains many forms, only one of which should be open at any point in time.

    Does anyone have any code suggestions on how to accomplish this, either globally or module by module? I have found that I can only execute Docmd.close …. in certain events, specifically in the After Update for a control. In most other events, I get a runtime error 2585 (??). I’m unclear as to when one can and cannot close a form. Would appreciate clarification on this.

    Thanks.

    Viewing 1 reply thread
    Author
    Replies
    • #564909

      You may use the following code to close all open forms except the one having the code.

      Dim intI As Integer

      ‘ Close all open forms except Me
      For intI = Forms.Count – 1 To 0 Step -1
      If Forms(intI).Name Me.Name Then ‘ Do not close Me
      DoCmd.Close acForm, Forms(intI).Name
      End If
      Next

      • #564913

        Claus:

        Thanks for the code. I inserted it as a private sub in the open event of a few modules. The first one worked fine. The second one resulted in a runtime error 2585 – “This action can’t be carried out while processing a form or report event”. This is the original error I was getting when I closed the forms individually.

        Do you, or anyone else, know why trying to close an open form in the open event of another form would produce this error?

        Thanks, again.

    • #564910

      You can use the following code to close all open forms in your application:

      Public Sub CloseOpenForms()
      
          Dim frm As Form
          Dim n As Integer
          n = Application.Forms.Count
          
          Do While n > 0
              For Each frm In Application.Forms
                  If IsLoaded(frm.NAME) Then
                      DoCmd.Close acForm, frm.NAME
                  End If
              Next frm
              n = Application.Forms.Count
          Loop
      
          Set frm = Nothing
          
      End Sub

      You need the Do Loop to refresh open forms count or else not all forms will be closed. The above uses the IsLoaded function found in Northwind.mdb:

      Function IsLoaded(ByVal strFormName As String) As Boolean
      ' Returns True if the specified form is open in Form view or Datasheet view.
          
          Const conObjStateClosed = 0
          Const conDesignView = 0
          
          If SysCmd(acSysCmdGetObjectState, acForm, strFormName)  conObjStateClosed Then
              If Forms(strFormName).CurrentView  conDesignView Then
                  IsLoaded = True
              End If
          End If
          
      End Function 

      These two procedures should be added to a standard module. To close all forms (including the one you are calling the procedure from) and open a new form, use this code for a event procedure:

      CloseOpenForms
      DoCmd.OpenForm "NameOfNewForm"

      Hope this helps.

      • #564914

        Mark:

        Thanks for the code. It seems to be working on my development machine (Win ME), but occasionally seems to go into a loop. That could something else entirely going on. I will need to test it further in the production environment – Win 2K.

        Thanks again.

        • #564918

          WSC:

          I was unable to reproduce loop error on my system. I’m running A2K SR-1A on WIN 98 SE. I did find one bug, though, in the database I was testing this with I have form that allows user to open more than one instance of same form. There are other forms that requery certain controls on the first form on their On_Close event. If I had several instances of first form open and one or more of these other forms open, it sometimes resulted in runtime error 2450, depending on sequence in which the forms were opened (IsLoaded equated to true, but name of form not found in Forms collection). I added error handling for this. If you are not opening multiple instances of same form this should not be an issue.

          • #564922

            Mark:

            I have not been able to reproduce the loop problem, even under Win ME. I will test further and post my results. For the time being, it looks stable enough for me to include in my other modules. I have included calls in seven modules so far.

            Thanks again.

            • #564939

              The code I gave you in my first reply works fine as long as you don’t have multiple instances of a form.
              From your originally description I understood that you would like to close all other forms but the one currently selected.
              This code makes sure NOT to close the form using/calling the code. Unlike other methods there is no reason to refresh form count variables and do multiple loops since my loop starts examining the highest numbered form.
              And since we are using the Forms collection (open forms only), there is no reason to check if a form is open.
              I have changed the code a little to make it easy for you to insert it in a module (not class module).
              It’s a function, so you don’t even have to call it from VBA code in a class module unless you have other things to do in the event chosen to call the function.
              In the events section of a form’s property definitions choose the Load or Open event and type directly: =CloseAllOtherOpenForms([Form])
              Be sure to type it eactly like that, i.e. don’t change [Form] to anything else.
              If you do have other VBA code for the chosen event include this line: Call CloseAllOtherOpenForms(Me)

              Public Function CloseAllOtherOpenForms(frm As Form) As Boolean
              Dim intI As Integer
              ‘ Close all open forms except the calling form
              For intI = Forms.Count – 1 To 0 Step -1
              If Forms(intI).Name frm.Name Then ‘ Do not close the calling form
              DoCmd.Close acForm, Forms(intI).Name
              End If
              Next intI
              End Function

            • #564942

              A small hint/correction to my prev. reply.
              Use the Load event. Not the Open event, since this will not execute if the form is already open.

            • #565017

              Claus:

              Thanks for the function. I will test both suggested methods and make a decision based on performance.

              Thanks again.

    Viewing 1 reply thread
    Reply To: Close All Open Forms (Access 2000 /2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: