Results 1 to 5 of 5
  1. #1
    Bruce is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    72

    Close All but One Form

    I've been using the following code to close all forms but 1 and it has been working fine. I added a new sub-form to my main display form and now the code doesn't function properly. My forms are indexed 0 - 3 and I need to keep index 0 open.:

    Dim intX As Integer
    intX = 1
    DoCmd.Close acForm, Forms(intX).Name

    intX = 1
    DoCmd.Close acForm, Forms(intX).Name


    Do While Forms.Count > 1
    DoCmd.Close acForm, Forms(1).Name
    Loop


    This use to work fine until I added the subform. Now, on-click event the code closes index #1, leaves 2 & 3 open (which are now numbered 1 & 2). If I hit the on-click event again, it closes 1 & 2 and leaves 0 open. I can't figure out why it takes 2x to close all the forms now while leaving index 0 open. I'm still learning VBA so excuse my lack of total understanding. Thanks!

  2. #2
    Bruce is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    72
    shortened to this:

    Dim intX As Integer
    intX = 1
    DoCmd.Close acForm, Forms(intX).Name

    Do While Forms.Count > 1
    DoCmd.Close acForm, Forms(1).Name
    Loop

    VB now returns an error of "The object you referred to is either closed or
    doesn't exist. but it does close the remaining two forms and leaves original form index 0 open.

    Is it possible to close multiple forms by name?

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I assume that this is your requirements:
    1) You have some forms.
    2) You want all but one of them if they are open.


    I have a simple mdb with the following forms:
    a) products: Form1
    b) customers: Form2
    c) Orders: Form3
    d) products
    e) stock: Form4

    A form control_Form will open as startup.It has two buttons:
    a) Open all forms. This will open all forms listed above.(The forms might cluster up at the left side of the screen after opening please drag them and arrange them as you want)
    b) Close all forms will close all forms except stock:Form4

    Code used:

    Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click
    Dim intCounter As Integer 'Incremental Counter to auto select forms
    Dim strFormName(3) As String 'Array declared contains the name of the forms
    strFormName(0) = "customers" 'FormNames
    strFormName(1) = "orders"
    strFormName(2) = "products"
    strFormName(3) = "stock'"

    For intCounter = 0 To 2 'Start loop
    If CurrentProject.AllForms(strFormName(intCounter)).I sLoaded Then 'Check if Form is open
    DoCmd.Close acForm, strFormName(intCounter) 'Close if open
    End If
    Next intCounter

    Exit_Command0_Click:
    Exit Sub

    Err_Command0_Click:
    MsgBox Err.Description
    Resume Exit_Command0_Click

    End Sub


    I have used an Array to determine the name of the forms.

    Scope of the Code:
    1) Close all forms other Control_Form and Stock:Form4

    Using Form Index is a wonderful Idea and eliminates the necessity of creating the Array that we had seen in the first Code. But the problem is to keep one form open. Suppose you open the Forms in following order

    Control_Form=0
    customers=1
    Orders=2
    products=3
    stock=4

    main difficulty of using Form index is that the forms are closed exactly in the order they were opened and assuming thet forms are not always opened in the same order Indexes makes the whole process very unpredictable. so I have used an if condition in the For loop. Code is as follows:


    Private Sub Command2_Click()
    On Error GoTo Err_Command2_Click
    Dim intFormCount As Integer
    Dim intCounter As Integer
    intCounter = Forms.Count - 1
    For intFormCount = intCounter To 0 Step -1 'Loop is Started
    If Forms(intFormCount).Name <> "stock" And Forms(intFormCount).Name <> "control_form" Then 'Condition to keep the two forms open
    DoCmd.Close acForm, Forms(intFormCount).Name
    End If
    Next

    Exit_Command2_Click:
    Exit Sub

    Err_Command2_Click:
    MsgBox Err.Description
    Resume Exit_Command2_Click

    End Sub


    Removing the if condition from this code will close all open forms.



    So the first instance names of forms are used and in the second form indexes.

    Please mark this thread solved if this solves youir problem.
    Find attached sample mdb

  4. #4
    Bruce is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    72
    MOST Excellent! Exactly what I was looking for. Using the name method worked great.

    Very greatful...this has been bugging me for 2 days now. Finally back on track!

    Thanks again!

  5. #5
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    glad to help.

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

Similar Threads

  1. Replies: 2
    Last Post: 01-29-2010, 11:33 AM
  2. Close DAP Connection
    By gfultz in forum Access
    Replies: 0
    Last Post: 10-16-2009, 10:11 AM
  3. Replies: 4
    Last Post: 09-10-2009, 03:09 AM
  4. close form
    By taylorosso in forum Forms
    Replies: 5
    Last Post: 08-25-2009, 12:18 PM
  5. Unable to close Query without saving
    By jhrBanker in forum Access
    Replies: 0
    Last Post: 06-08-2009, 05:09 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