Reputation: 19
I'm making a database for an assignment.
When you log in it checks whether the user is an admin. An admin has access to different forms.
My logout button opens up the login form and they can't do anything else until they sign in.
If an admin hasn't closed their admin-specific forms when they log out then a non-admin can log in onto their account and the forms the admin failed to close will still be open.
How do I close all open forms/reports etc. when a button is pressed to log out?
Upvotes: 0
Views: 3598
Reputation: 27634
Since we are closing forms during the loop, and therefore removing them from the Forms collection, this sort of loop is needed:
Public Sub Logout()
Dim F As Access.Form
Dim i As Long
' Loop all open forms, from last to first, to avoid problems due to closing forms
' (removing them from the Forms collection) in the loop
For i = Forms.Count - 1 To 0 Step -1
Set F = Forms(i)
' Close all forms except the login form
If F.Name <> "frmLogin" Then
DoCmd.Close acForm, F.Name
End If
Next i
End Sub
And of course the same for the Reports
collection.
In general, an easier way to loop collections is with For Each
, but this doesn't work if collection members are removed during the loop.
Thanks Blackbath and DecoMartins for pointing that out.
Dim varF As Variant
' Loop all open forms
For Each varF In Forms
If varF.Name <> "frmLogin" Then
' Do something with varF
End If
Next varF
Upvotes: 3