Public Sub CloseModules()
'*******************************************
'Purpose: Close all open modules, forms
' and reports. Useful after
' performing a search within
' modules.
'Coded by: raskew
'Inputs: from debug window: call closemodules
'*******************************************
Dim db As Database
Dim rs As Recordset
Dim strHold As String
Dim strSQL As String
Dim intType As Integer
Dim n As Integer
Set db = CurrentDb
'Standard module = -32761; Report = -32764; Form = -32768
strSQL = "SELECT MSysObjects.Name, Switch([type]=-32761,5,[type]=-32764,3,True,2) AS MyType" _
& " FROM MSysObjects" _
& " WHERE (((MSysObjects.Type) In (-32761,-32764,-32768)))" _
& " ORDER BY Switch([type]=-32761,5,[type]=-32764,3,True,2);"
Set rs = db.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.MoveFirst
If n > 0 Then
Do While Not rs.EOF
strHold = rs!name
intType = rs!MyType
If (SysCmd(acSysCmdGetObjectState, intType, strHold) And acObjStateOpen) <> False Then
docmd.Close intType, strHold, acSaveYes
End If
rs.MoveNext
Loop
End If
rs.Close
db.Close
Set db = Nothing
End Sub