Close form when something else is clicked

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

Guest

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.
 
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.
 
Have you tested it yet?

It seems this line
For Each oForm In Forms

Should probably be
For Each oForm In CurrentProject.AllForms
 
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.
 

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