Solved

Close all forms except for active form

Posted on 2007-03-27
19
730 Views
Last Modified: 2013-11-28
Need code that will close all forms except for active form.  I tried placing the following in mutiple locations on the form without success.  Tried it on Load, on Activate, got focus, etc. Activform name = FE_SRForm

    If IsFormLoaded("FS_SelectSR") Then
        DoCmd.Close acForm, "FS_SelectSR", acSaveNo
    End If
    If IsFormLoaded("Switchboard") Then
        DoCmd.Close acForm, "Switchboard", acSaveNo
    End If
0
Comment
Question by:Karen Schaefer
  • 6
  • 4
  • 3
  • +2
19 Comments
 
LVL 84
ID: 18804199
By ActiveForm, do you mean the form where this code is running?

If so, then this will do the trick I believe:

Dim frm As Form

For Each frm In Forms
  If frm.Name <> Me.Name Then DoCmd.Close acForm, frm.Name
Next frm

Not sure where you'd put this, as I don't know where you want to trigger it. Typically this would be on a Button click
0
 

Author Comment

by:Karen Schaefer
ID: 18804218
I have two forms - Form 1 has on double click of list box - opens form2 based on selection - I want to close form1 once form2 has opened and also check to make sure no other forms are currenly open except for form2.

K
0
 
LVL 84
ID: 18804303
If this is the ONLY place you want to close this, then I'd try this code in the GotFocus event of Form2 ... or perhaps on the Activate event.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 84
ID: 18804309
Or, you could always do this AFTER your code on Form1 has opened Form2

Dim frm As Form

For Each frm In Forms
  If frm.Name <> "NameOfForm2" Then DoCmd.Close acForm, frm.Name
Next frm
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 168 total points
ID: 18804314
Whooops ... probaby need to do this as well:

Dim frm As Form

For Each frm In Forms
  If frm.Name <> "Form2" OR frm.Name <> Me.Name Then DoCmd.Close acForm, frm.Name
Next frm

'/now close Form1
DoCmd.Close acForm, "Form1"
0
 

Author Comment

by:Karen Schaefer
ID: 18804400
what is the code for active screen - so that I can pass the active form's name.
k
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18804415

dim frm as form

set frm=Screen.ActiveForm
0
 
LVL 75
ID: 18804422
This will close all open forms ... except the 'active form'.

As  LSM said ... not sure where you want to put this ... maybe in a VBA module ... and then you could call it from any form via a command button?

    Do While Forms.Count > 0
        if Forms(0).Name <> Screen.ActiveForm then   DoCmd.Close acForm, Forms(0).Name
    Loop

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18804437

 If Application.Screen.ActiveControl.Name = "form1" Then
        MsgBox "yes"
    Else
        MsgBox "no"
    End If
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 166 total points
ID: 18804461
you can use this to get the activeform name

MsgBox Application.Screen.ActiveForm.Name

correction,
 If Application.Screen.ActiveForm.Name = "form1" Then
        MsgBox "yes"
    Else
        MsgBox "no"
    End If
0
 

Author Comment

by:Karen Schaefer
ID: 18804464
Which way gives me the cleaness/seamless way to open and close forms and only have the active form open.

k
0
 
LVL 75
ID: 18804495
Well ... we would probably say 'My Way', lol ... so ... why not just try each and see ... should be a good test ... and each is simple enough to quickly try ... seriously.

mx
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18804518
this codes called from form1

Private Sub Command0_Click()
DoCmd.OpenForm "Form2"
DoCmd.OpenForm "Form3"
MsgBox Application.Screen.ActiveForm.Name
End Sub

will give you  Form3  in the message box,
now figure how do you want to use  Application.Screen.ActiveForm.Name
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18804621
K,
place this code in the load event of the form you want to remain open

Private Sub Form_Load()
Dim frm As AccessObject
For Each frm In CurrentProject.AllForms
    If frm.Name <> Me.Name Then DoCmd.Close acForm, frm.Name
Next
End Sub
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18804634
you can also use the open event of the form
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 166 total points
ID: 18804658
And a correction to my last post:

    Do While Forms.Count > 0
        if Forms(0).Name <> Screen.ActiveForm.Name then   DoCmd.Close acForm, Forms(0).Name
    Loop

>> I left Name off of Screen.ActiveForm
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18805688
Just a quick question....and it is probably a silly question...does your active form happen to have a subform? I only ask because....well....it matters.
J
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question