Close any form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 

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

Back
Top