When does a form lose focus?

Sprocket

Registered User.
Local time
Today, 16:37
Joined
Mar 15, 2002
Messages
70
When does a form lose focus?

I have a point in my routine where I have two forms open at the same time.

Form_1 and Form_2

I want Form_1 to close when it loses focus, so I put the code:

DoCmd.Close in the On Lost Focus event of Form_1 but it doesn't work.
I can move to Form_2 and enter data but Form_1 remains stubornly open.

If I put the the same code in any of the forms field controls On Lost Focus events and move to another field the form closes, so why not when I put it in the Form's own On Lost Focus event.

I can also put code in the On Got Focus event for Form_2 that references Form_1 and close it that way but I am intrigued to know why it won't work From the On Lost Focus event of Form_1
 
Your form, most likely, will never receive the focus and therefore will never lose the focus either.

The GotFocus and LostFocus events for a form will only trigger if there is no control on the form that can receive the focus.

So, if you have one textbox on a form and the cursor can be set in it then the form won't receive the focus but the textbox will immediately. If that textbox is not enabled then the focus has nowhere else to go but the form and that's the only time a form can get or lose the focus.
 
OK - so now I know the reason (doesn't seem very logical but then a lot of things that MS products do or don't do aren't very logical). Thanks for that.

So my next question is - What is the best way to close a form when it is no longer required?

There seems to be lots of ways to do it but which is best (less likely to cause any problems) using automation rather than clicking the close button that is.


Cheers..........Sprocket
 
It's not illogical when you think about it. If only one object can have the focus then it is right that the first enabled control should get the focus and not the form itself.

Rather than use the LostFocus event though, you want to use the DeActivate event for what you have already stated you are trying to attempt.
 

Users who are viewing this thread

Back
Top Bottom