Solved

Close all forms except for active form

Posted on 2007-03-27
19
726 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
 
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 119

Expert Comment

by:Rey Obrero
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 18804437

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

Assisted Solution

by:Rey Obrero
Rey Obrero 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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

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

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now