Solved

Close all forms except for active form

Posted on 2007-03-27
19
731 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to Use an Access DB to run a job forever 6 59
IIF syntax in MS Access 1 62
Null or "" 28 52
access query - filter field for particular number format 3 28
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

763 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