(the Close all open Modules
Advent Day 23 Ms Access Gurus

VBA > Module > Close Open

Close open standard modules. Optionally keep a module open by passing a module name or determining the active module (default), or close them all.

Specify whether to save now, not save, or prompt (default) to save later.

This is especially nice for developers so you can then open just what you want to focus on, switch faster, and realistically tile windows.

Screen shot

It is nice to close everything ... except maybe a module you want to keep open. To close everything except what you are looking at:

  1. make the module you want to keep open active.
  2. press Ctrl-G for the debug window (if it is this module, you can use the launcher procedure instead of the next step, if you want)
  3. Type or paste: CloseOpenModules — and press Enter

By default, the active module is kept open, and all other standard modules are closed.

Examples


Close all open standard modules, except the active module. If there are unsaved changes, then prompt to save later on, when you save something else.

Call CloseOpenModules()

or

CloseOpenModules

This does the same as above, except it also captures the string return and shows it in a message box. Go to CloseOpenModules_Launch

Call CloseOpenModules_Launch()

or

CloseOpenModules_Launch

Close all, Save all

CloseOpenModules(1, , "")

Close all, don't save

CloseOpenModules(2, , "")

try this in the debug window -- it closes everything (prompts you to save later if there were unsaved changes), and gives you a message telling you how many modules were closed.

MsgBox CloseOpenModules() & " modules closed (except active)",, " Done"

FYI: name of the active module

Application.VBE.ActiveCodePane.CodeModule.Name

Logic

There is no error handler.

Dimension obj for the module object, and dimension other variables. Initalize the number closed, iCount, to be zero (0), since nothing has happened yet.

If "~" was specified for psModuleKeepOpen, then determine the name of the active module using Application.VBE.ActiveCodePane.CodeModule.Name

Use the Choose function to make sure that the save parameter is acceptable. If not, set to 0 (zero), the default, to Prompt (later) for save. Then use the Choose function again, to describe the save parameter in words.

sMsg, sMsg1, and sMsg2 will be reported at the end. While the code is running, they construct messages for the debug window ... so if you are watching it, you can see what is happening in real-time.

Loop

Loop though all the modules using the CurrentProject.AllModules collection.

For each module in the loop, store its Name in sName. If the module is loaded and it is not the module to keep open (if there is one), then close it using

DoCmd.Close acModule, sName, piSave

In the loop, iCount keeps track of how many modules are being closed, and sMsg is a running string of information to be returned.

Returns

Return the message string, sMsg, in the psRETURNmsg parameter, in case the calling program wants it.

Set the function return to the number of modules that were closed.

Parameters

Logic: CloseOpenModules_Launch

This is a launcher program for CloseOpenModules to close all except whatever is active. Prompt to save later if necessary.

Code

' Module Name: mod_CloseModules_s4p
'*************** Code Start *****************************************************
' Purpose  : close open standard modules, optionally keep one open
' Author   : crystal (strive4peace)
' Return   : Integer (and String)
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'--------------------------------------------------------------------------------
'                              CloseOpenModules
'--------------------------------------------------------------------------------
'
Public Function CloseOpenModules( _
   Optional piSave As Integer = 0 _
   , Optional psRETURNmsg As String _
   , Optional psModuleKeepOpen As String = "~" _
   ) As Integer
'...181223 s4p
'note: this only closes standard modules
'close class modules by looping and doing, for instance:
'  DoCmd.Close acModule, Forms!MyForm.Module
'prints information to Debug window

   'PARAMETERS
   '  piSave
   '     0 = Prompt to Save (Default) -- close, prompt to save later if anything was changed
   '     1 = Save
   '     2 = No save -- close without saving
   '
   '  psRETURNmsg is treated as a RETURN
   '     to report a message of what was done
   '
   '  psModuleKeepOpen is the name of module to keep open.
   '     "~" (Default) is close all except the active module
   '     "" is close everything
   '
   'RETURN
   '  number of modules closed
   
   Dim obj As Object
   
   Dim sName As String _
      , iCount As Integer _
      , sMsg As String _
      , sMsg1 As String _
      , sMsg2 As String _
      , sChoose As String
   
   iCount = 0
   
   If psModuleKeepOpen = "~" Then
      'active module
      psModuleKeepOpen = Application.VBE.ActiveCodePane.CodeModule.Name
   End If
   
   If IsNull(Choose(piSave + 1, 0, 1, 2)) Then
      'piSave was not 0, 1, or 2
      piSave = 0 'Default = prompt if there are unsaved changes
   End If
   
   'Choose starts at 1, so add 1 to the save parameter
   sChoose = Choose(piSave + 1, "Prompt", "Save", "Don't save")
   
   sMsg1 = "----- Close modules ----- " _
      & sChoose & " ----- " & Now()
   sMsg = ""
   
   If psModuleKeepOpen <> "" Then
      sMsg1 = sMsg1 _
      & vbCrLf & "  keep open: " & psModuleKeepOpen _
      & vbCrLf & "  close:"
   End If
   
   Debug.Print sMsg1
   
   'loop through all the modules
   For Each obj In CurrentProject.AllModules

      With obj
         sName = .Name
         'don't close specified module to keep open, and
         'don't close what isn't loaded
         If sName <> psModuleKeepOpen And .IsLoaded Then
            DoCmd.Close acModule, sName, piSave
            iCount = iCount + 1
            sMsg2 = "(" & iCount & ")" & sName & "  "
            Debug.Print Space(5) & sMsg2
            sMsg = sMsg & sMsg2
         End If
      End With 'obj
      
   Next obj 'CurrentProject.AllModules
   
   sMsg2 = "Closed " & iCount & " modules"
   Debug.Print sMsg2
   
   sMsg = sMsg1 & vbCrLf _
      & Space(10) & sMsg _
      & vbCrLf & vbCrLf & sMsg2

   psRETURNmsg = sMsg  'return this in case caller wants it

   CloseOpenModules = iCount
   
   Set obj = Nothing
   
End Function

'--------------------------------------------------------------------------------
'                              CloseOpenModules_Launch
'--------------------------------------------------------------------------------
'
Sub CloseOpenModules_Launch()
   'Click HERE and press F5 to Run!
   Dim sMsg As String _
      , iCount As Integer

   'Close all except this one. Prompt to save later.
    iCount = CloseOpenModules(0, sMsg) ' 0 is the default so not necessary to specify 

   MsgBox sMsg, , "Done. Closed " & iCount & " modules"
End Sub
'
' LICENSE
'   You may freely use and share this code 
'     provided this license notice and comment lines are not changed;
'     code may be modified provided you clearly note your changes.
'   You may not sell this code alone, or as part of a collection,
'     without my handwritten permission.
'   All ownership rights reserved. Use at your own risk.
'   ~ crystal (strive4peace)  www.MsAccessGurus.com
'*************** Code End *******************************************************

Back Story

Focus on what you need to see

Tons of stuff is open and you can't find anything! Maybe all you want to do is compare the code in different modules.

Solution: close all the modules, unless you can find something you want to keep open quickly, then keep that open. Then just open the modules you want and tile them (ie: Window > Tile Vertically).

References

Docs / Office VBA Reference / Access / Object model / Application object / Properties / VBE

Help: Application.VBE property (Access)

Docs / Office VBA Reference / Language reference / Reference / Visual Basic Add-in Model / Properties

Help: Properties (Visual Basic Add-In Model) for ActiveCodePane, CodeModule and more

Docs / Office VBA Reference / Access / Object model / Application object / Properties / CurrentProject

Help: Application.CurrentProject property

Docs / Office VBA Reference / Access / Object model / CurrentProject / Properties

Help: CurrentProject.AllModules property

Docs / Office VBA Reference / Access / Object model / Properties / IsLoaded

Help: AccessObject.IsLoaded property

Docs / Office VBA Reference / Language reference / Reference / Functions / Choose

Help: Choose function

Share with others

here's the link to copy:

https://MsAccessGurus.com/VBA/Code/Mod_CloseOpenModules.htm

Do you have something to say or share?

It is interesting to hear from you. Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more (there is always more)?

Some of you write to say thanks and tell me what you're doing with Access ... its nice to get an echo back. I want you and others to be good with Access, and other Office applications like Excel, Word, and PowerPoint ... and Windows. Take advantage of the strengths in each to manage your information wisely.

Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.

When we communicate, collaborate, and appreciate, we all get better. Thank you. Email me at info@msAccessGurus.com

Goto Top