close forms prior to previewing/printing

J

jlute

I have a criteria form from which I preview/print reports and need to
close all open forms EXCEPT the criteria form and a main menu prior to
previewing/printing.

What I'd like to do is click the preview or print button which will
close the forms prior to executing other commands. Is there a simple
code to do this? I've searched through the groups and have found some
very complex codes.

Thanks!!!
 
D

Dennis

Dim i As Integer
Dim strForm as String

For i = 1 To CurrentProject.AllForms.Count
If CurrentProject.AllForms(i - 1).IsLoaded Then
strForm = CurrentProject.AllForms(i - 1).Name
If strForm <> "criteria form" And strForm <> "main menu" Then
DoCmd.Close acForm, strForm, acSaveNo
End If
End If
Next i
 
D

Dirk Goldgar

I have a criteria form from which I preview/print reports and need to
close all open forms EXCEPT the criteria form and a main menu prior to
previewing/printing.

What I'd like to do is click the preview or print button which will
close the forms prior to executing other commands. Is there a simple
code to do this? I've searched through the groups and have found some
very complex codes.


John -

Here's a routine you can call to close all forms except a list you specify
as arguments when you call it:

'----- start of code -----
Sub CloseAllFormsExcept(ParamArray ExceptForm())

' Copyright (c) 2008, DataGnostics LLC
' License is granted to use in your application, so long as
' the copyright notice remains unchanged.

Dim lngFrm As Long
Dim intX As Integer
Dim blnPreserve As Boolean

' Close all open forms except those in the list.

For lngFrm = Forms.Count - 1 To 0 Step -1
With Forms(lngFrm)
blnPreserve = False
For intX = LBound(ExceptForm) To UBound(ExceptForm)
If .Name = ExceptForm(intX) Then
blnPreserve = True
Exit For
End If
Next intX
If blnPreserve Then
' Spare this form
Else
DoCmd.Close acForm, .Name
End If
End With
Next lngFrm

End Sub

'----- end of code -----
 
J

jlute

Thanks, Dennis! That's a simple and nifty one!

Dim i As Integer
Dim strForm as String

    For i = 1 To CurrentProject.AllForms.Count
        If CurrentProject.AllForms(i - 1).IsLoaded Then
            strForm = CurrentProject.AllForms(i - 1).Name
            If strForm <> "criteria form" And strForm <> "mainmenu" Then
                DoCmd.Close acForm, strForm, acSaveNo
            End If
        End If
    Next i







- Show quoted text -
 
J

jlute

Hi, Dirk! Long time no see!

Thanks - this is a powerful solution, too! Not sure which I'm going to
use but I certainly appreciate the fact that there's always such
diversity in responses here!
 

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