Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Close All Open Forms (Access 2000 /2002)

    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.
    Carol W.

  2. #2
    Star Lounger
    Join Date
    Dec 2001
    Location
    Fredensborg, Denmark
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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

  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Close All Open Forms (Access 2000 /2002)

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

    <pre>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</pre>

    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:
    <pre>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 </pre>

    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:
    <pre>CloseOpenForms
    DoCmd.OpenForm "NameOfNewForm"</pre>

    Hope this helps.

  4. #4
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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.
    Carol W.

  5. #5
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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.
    Carol W.

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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.

  7. #7
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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.
    Carol W.

  8. #8
    Star Lounger
    Join Date
    Dec 2001
    Location
    Fredensborg, Denmark
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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

  9. #9
    Star Lounger
    Join Date
    Dec 2001
    Location
    Fredensborg, Denmark
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    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.

  10. #10
    3 Star Lounger
    Join Date
    Dec 2001
    Location
    Nevada, USA
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Close All Open Forms (Access 2000 /2002)

    Claus:

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

    Thanks again.
    Carol W.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •