Close any form

A

acores

Hi.
I want to create a macro that closes ANY form opened when I open a new one.
Is it possible?
Thank you in advance.
 
D

Douglas J. Steele

The Forms collection only contains open forms, so you could loop through it,
closing everything except the current form.

Dim intLoop As Integer

For intLoop = (Forms.Count - 1) To 0 Step -1
If Forms(intLoop).Name <> "NameOfCurrentForm" Then
DoCmd.Close acForm, Forms(intLoop).Name, acSaveNo
End If
Next intLoop

The reason for looping from the end to the front is that if you went from 0
to the end, when you deleted Forms(0), the previous Forms(1) would then
become Forms(0). When you hit the Next statement, you'd move from Forms(0)
to Forms(1), meaning that you'd never delete the form that started as form
1.

Depending on where you're calling this form, how you determine
"NameOfCurrentForm" will change.
 
Top