From: jlute on
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!!!
From: Dennis on
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

"jlute(a)marzetti.com" wrote:

> 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!!!
>
From: Dirk Goldgar on
<jlute(a)marzetti.com> wrote in message
news:55b99c18-0d12-44c5-b9b5-2822c3472a23(a)l42g2000hsc.googlegroups.com...
>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 -----

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

From: jlute on
Thanks, Dennis! That's a simple and nifty one!

On Jun 30, 10:33 am, Dennis <Den...(a)discussions.microsoft.com> wrote:
> 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
>
>
>
> "jl...(a)marzetti.com" wrote:
> > 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!!!- Hide quoted text -
>
> - Show quoted text -

From: jlute on
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!

On Jun 30, 10:50 am, "Dirk Goldgar"
<d...(a)NOdataSPAMgnostics.com.invalid> wrote:
> <jl...(a)marzetti.com> wrote in message
>
> news:55b99c18-0d12-44c5-b9b5-2822c3472a23(a)l42g2000hsc.googlegroups.com...
>
> >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 -----
>
> --
> Dirk Goldgar, MS Access MVPwww.datagnostics.com
>
> (please reply to the newsgroup)