Solved

Close all forms except for active form

Posted on 2007-03-27
19
725 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

760 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

20 Experts available now in Live!

Get 1:1 Help Now