Solved

Close all forms except for active form

Posted on 2007-03-27
19
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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 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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

738 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