Best Way to Loop Through and Close Certain Forms

padlocked17

Registered User.
Local time
Yesterday, 18:23
Joined
Aug 29, 2007
Messages
276
I'm using the following piece of code air code. How would I change this to loop through and close certain forms if they are open.

I would you an If ElseIf, but I need each statement to be evaluated so that if multiple forms are open, each one that I specify will be closed.

Code:
Select Case CurrentProject.AllForms("NameOfForm").IsLoaded
        Case "frmEnroll"
            Debug.Print "Enrollment Form"
        Case "frmPPEnrollment"
            Debug.Print "PP Enrollment Form"
        Case Else
            Debug.Print "Some Other form"
        End Select
 
Hello:

You would use the "ALL Forms" collection to do this:

Sub AllForms()
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
' Search for open AccessObject objects in AllForms collection.
For Each obj In dbs.AllForms
If obj.IsLoaded = True Then
' Print name of obj.
Debug.Print obj.Name
End If
Next obj
End Sub

Search your VBA help section for ALLForms. It explains it pretty well!

Regards
Mark
 
Russ, do you want to close multiple forms at once, or just one form now and some other form later? And you only want to close the form if it is open? If it is just one form now and another later, just close it. If it is not open, Access does not care. There is no error message or anything. Access closes the form if it is open, and if the form is not open, Access just executes the close command and keeps going.

Is there something else you want to do along with closing one or more forms?
 
Is there something else you want to do along with closing one or more forms?

Nope, just wanting to close several forms.

I guess I've always had a hard time writing in code that executes without condition.

Knowing that Access will just process w/o error means I'll just add that in there now.

I guess I was over analyzing this one. But that's why all you nice folks are here.

Thanks for both of the replies. I feel like the All Forms will come in handy and it was one more feature I had never heard about.

Thanks!
 

Users who are viewing this thread

Back
Top Bottom