Close form when something else is clicked

X

XP

I have just a few forms in a very small DB.

Suppose the user has Form1 open, then clicks away or off of the current
form, is there a way to make that form (Form1) close automatically?

I tried the "On Lost Focus" property but that didn't seem to work, unless I
coded it wrong.

Can someone please help me out? Thanks.
 
X

XP

Actually, I just coded a solution, but I just wanted to get the opinions of
the forum:
I wrote a function which uses one argument for the form you want to keep
open. The function loops thru all open forms and closes them all except a
"Main" form that I want left open and the one calling the function. I just
call this function from each form and everything is kept clean. This seems to
work well:

Public Function CloseForms(argFormKeepOpen As String)
'CLOSE ALL OPEN FORMS EXCEPT 'MAIN' AND THE ONE SPECIFIED IN THE ARGUMENT
Dim oForm As Object
For Each oForm In Forms
If argFormKeepOpen <> oForm.Name And oForm.Name <> "frmMainForm" Then
DoCmd.Close acForm, oForm.Name
Next
End Function

Opinions? Improvements? All welcomed, thanks.
 
K

Klatuu

Have you tested it yet?

It seems this line
For Each oForm In Forms

Should probably be
For Each oForm In CurrentProject.AllForms
 
X

XP

Yes, actually it runs perfectly, however, your code would seem to be more
robust in its form and I will adopt it. Thanks for your input.
 
Top