(the
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.
It is nice to close everything ... except maybe a module you want to keep open. To close everything except what you are looking at:
By default, the active module is kept open, and all other standard modules are closed.
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 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
In the loop, iCount keeps track of how many modules are being closed, and sMsg is a running string of information to be returned.
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.
psRETURNmsg is passed by reference (ByRef is the default if not specified).
By reference means that a new variable is not created if it was passed ... so if the value changes, the variable value changes in the calling program too.
ByRef variables can be used to return more information. Subs, which don't return a value, can also use references to variables.
This is a launcher program for CloseOpenModules to close all except whatever is active. Prompt to save later if necessary.
' 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 *******************************************************
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).
Help: Application.VBE property (Access)
Help: Properties (Visual Basic Add-In Model) for ActiveCodePane, CodeModule and more
Help: Application.CurrentProject property
Help: CurrentProject.AllModules property
Help: AccessObject.IsLoaded property
here's the link to copy:
https://MsAccessGurus.com/VBA/Code/Mod_CloseOpenModules.htm
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