Close form when it loses focus



Hello All

I have a form "main_table_subform_master" which I would like to close
automatically whenever the user clicks elsewhere on the screen (i.e. not on
the form in question).
I thought that putting the following code as the LostFocus event would work:

DoCmd.Close acForm, "main_table_subform_master", acSaveYes

.... but nothing happens when the user clicks elesewhere.
Why not, and what can I do?

Hope someone can help.
Many thanks
Leslie Isaacs




Why would you want to allow a form to close when the user clicks elsewhere?
You are assuming that they are done entering data or whatever the form is
used for. What if they accidentally click outside of the form? It would be
better to force the user to click a close button and make it modal and a


I really don't know of any way to do it. You are not getting an error
because the form's lost focus event will almost never fire. That is because
a form never gets the focus unless there are no controls on the form capable
of accepting the focus.

The only event that would fire would be the DeActivate event, but if you try
to close the form from that event, you will get an error saying you can't
perform the action while processing a form event.

Just as an aside, I think you may misunderstand the Save arugment in the
close method. It does not apply to the form's recordset. Any unsaved
changes to a form's data are always changed unless you explicitly prevent it
from doing so. It applies to any design changes made to the form itself.

Leslie Isaacs

Hello "Rockn"
Thanks for your reply.
Without boring you with the details, trust me that it is highly desirable to
ensure this form closes whenever the user goes elsewhere on the screen: much
better that hoping the user will use the 'close' button on the form! Making
the form modal would also work, but my general philosophy is to reduce to a
minimum the amount of clicking a user must perform.
Do you think it can be done?
Thanks again
Les (from home PC - hence difference username!)



Leslie Isaacs

Hello "Klatuu"

Thanks for your reply.
Oh well - it was worth a try!
I guess I will have to settle for making the form modal and forcing the user
to use the Close button on the form: but I always like to reduce the number
of required mouseclicks to an absolute minimum, and OnLostFocus > CloseForm
would have been neater.
Thanks also for your comment about the Save arugment in the close method. I
will remove it!

Thanks again

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