HOW TO CLOSE ALL FORMS EXCEPT ONE FORM

I

Irshad Alam

I HAVE ABOUT 35 FORMS AND ABOUT 42 REPORTS AND THESE BOTHS
ARE GENERALLY USED BY THE USER.

NOW I AM LOOKING FOR A SOLUTION THAT WHEN EVER A USER WILL
TRY TO OPEN A REPORT OR ANY FORM, ALL THE REST OF THE
FORMS AND REPORTS WILL BE CLOSED EXCEPT THE
MAINSWITCHBOARD FORM AND THE FORM/REPORT USER IS TRYING TO
OPEN.

I DONT WANT TO CLOSE THE MAIN SWITCH BOARD FORM, BECAUSE I
HAVE ATTACHED A CODE TO QUIT DATABASE WHEN THE
MAINSWITCHBOARD FORM IS CLOSED.

I WANT TO DO ABOVE, MY APPLICATION IS HAVE DESEIGNED IN
MENU WAY, THEREFORE, USERS ARE ABLE TO OPEN MORE THAN
SEVEN TO EIGHT FORMS, WITHOUT CLOSING THE OTHERS.

THEREFORE, I WANT A SOLUTION THAT WHEN A USER WILL SELECT
A FORM FROM THE MENU, OTHER FORMS, REPORTS WILL BE CLOSE
EXCEPT THE FORM USER IS OPENING AND THE MAIN SWITCH BOARD
FORM.

PLEASE ADVISE.
 
D

Dirk Goldgar

Irshad Alam said:
I HAVE ABOUT 35 FORMS AND ABOUT 42 REPORTS AND THESE BOTHS
ARE GENERALLY USED BY THE USER.

NOW I AM LOOKING FOR A SOLUTION THAT WHEN EVER A USER WILL
TRY TO OPEN A REPORT OR ANY FORM, ALL THE REST OF THE
FORMS AND REPORTS WILL BE CLOSED EXCEPT THE
MAINSWITCHBOARD FORM AND THE FORM/REPORT USER IS TRYING TO
OPEN.

I DONT WANT TO CLOSE THE MAIN SWITCH BOARD FORM, BECAUSE I
HAVE ATTACHED A CODE TO QUIT DATABASE WHEN THE
MAINSWITCHBOARD FORM IS CLOSED.

I WANT TO DO ABOVE, MY APPLICATION IS HAVE DESEIGNED IN
MENU WAY, THEREFORE, USERS ARE ABLE TO OPEN MORE THAN
SEVEN TO EIGHT FORMS, WITHOUT CLOSING THE OTHERS.

THEREFORE, I WANT A SOLUTION THAT WHEN A USER WILL SELECT
A FORM FROM THE MENU, OTHER FORMS, REPORTS WILL BE CLOSE
EXCEPT THE FORM USER IS OPENING AND THE MAIN SWITCH BOARD
FORM.

PLEASE ADVISE.

Please don't post all in capital letters. It's hard to read, and it
feels like you're shouting at us.

Below is a function that will close all open objects except one you
specify:

'----- start of code -----
Sub CloseAllExcept(ObjectType As AcObjectType, ObjectName As String)

Dim ao As AccessObject
Dim lngX As Long

' Close all open tables
For Each ao In CurrentData.AllTables
With ao
If .IsLoaded Then
If ObjectType = acTable And ObjectName = ao.Name Then
' Spare this one
Else
DoCmd.Close acTable, ao.Name
End If
End If
End With
Next ao

' Close all open queries
For Each ao In CurrentData.AllQueries
With ao
If .IsLoaded Then
If ObjectType = acQuery And ObjectName = ao.Name Then
' Spare this one
Else
DoCmd.Close acQuery, ao.Name
End If
End If
End With
Next ao


' Close all open macros
For Each ao In CurrentProject.AllMacros
With ao
If .IsLoaded Then
If ObjectType = acMacro And ObjectName = ao.Name Then
' Spare this one
Else
DoCmd.Close acMacro, ao.Name
End If
End If
End With
Next ao

' Close all open modules
For lngX = Modules.Count - 1 To 0 Step -1
With Modules(lngX)
If ObjectType = acModule And ObjectName = .Name Then
' Spare this one
Else
DoCmd.Close acModule, .Name
End If
End With
Next lngX

' Close all open data access pages
For lngX = DataAccessPages.Count - 1 To 0 Step -1
With DataAccessPages(lngX)
If ObjectType = acDataAccessPage And ObjectName = .Name Then
' Spare this one
Else
DoCmd.Close acDataAccessPage, .Name
End If
End With
Next lngX

' Close all open reports
For lngX = Reports.Count - 1 To 0 Step -1
With Reports(lngX)
If ObjectType = acReport And ObjectName = .Name Then
' Spare this one
Else
DoCmd.Close acReport, .Name
End If
End With
Next lngX

' Close all open forms
For lngX = Forms.Count - 1 To 0 Step -1
With Forms(lngX)
If ObjectType = acForm And ObjectName = .Name Then
' Spare this one
Else
DoCmd.Close acForm, .Name
End If
End With
Next lngX

End Sub
'----- end of code -----

The function doesn't attempt to close objects of types that only occur
in ADPs, like Views, Stored Procedures, and Database Diagrams.

You could call this function just before you open any other object than
your main form, telling it to close all objcts except your main
switchboard form. Something like this:

CloseAllExcept acForm, "Switchboard"
DoCmd.OpenForm "SomeOtherForm"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top