LostFocus not triggered when leaving subform

K

kheisler6

There are several posts about this problem, but no solutions.

I would like to trigger some code when a subform loses focus. The
LostFocus event doesn't trigger when this happens.

For example, if the user is in the subform, but he then tries to click
on a button on the main form that simply pops up a message, I would
like an event to trigger if a certain field in the subform is Null
(e.g., "Please enter a date in the subform before doing anything
else!").

Any ideas? Thanks.

Kurt
 
R

Rick Brandt

There are several posts about this problem, but no solutions.

I would like to trigger some code when a subform loses focus. The
LostFocus event doesn't trigger when this happens.

For example, if the user is in the subform, but he then tries to click
on a button on the main form that simply pops up a message, I would
like an event to trigger if a certain field in the subform is Null
(e.g., "Please enter a date in the subform before doing anything
else!").

Any ideas? Thanks.

Kurt

Perhaps the problem is because a subform control doesn't have a LostFocus
event. It does have Enter and Exit events though.

The form shown inside the subform control has a LostFocus event but that
doesn't apply when it is used as a subform. Move your code to the Exit
event of the subform control and you should be fine.
 
K

kheisler6

Ahh ... I see what the problem is.

I originally had the code on the Exit event and it wasn't triggering.
Here's why:

When the user clicks on a field control (text box, combo box, etc.) or
command button on the main form, the Exit code triggers.

But many of my controls on the main form are labels with on click code
behind them. (I prefer using labels with on click code, as opposed to
command buttons, because I think labels provide a cleaner look.) But
when a user clicks on one of these labels, the Exit code isn't
triggered. (It's as if the labels aren't really "pressed down" like
command buttons. Moving the code from on click to mouse down doesn't
fix it.)

I had the same problem when I used a label called "Delete" for each row
on a continuous form. No matter what record the user was on, clicking
Delete wouldn't detect the actual record that had the focus. Changing
it from a label to a command button fixed the problem.

How can I get Access to handle labels with on click code the same way
it handles command buttons with the same code?

Kurt
 
R

Rick Brandt

Ahh ... I see what the problem is.

I originally had the code on the Exit event and it wasn't triggering.
Here's why:

When the user clicks on a field control (text box, combo box, etc.) or
command button on the main form, the Exit code triggers.

But many of my controls on the main form are labels with on click code [snip]

How can I get Access to handle labels with on click code the same way
it handles command buttons with the same code?

The first line of code behind the label clicks needs to set focus to some
object on the main form. Since labels can't have focus they do not trigger
that themselves. An alternative would be to just call the subform Exit code
from the label-click code.
 
D

Dirk Goldgar

Ahh ... I see what the problem is.

I originally had the code on the Exit event and it wasn't triggering.
Here's why:

When the user clicks on a field control (text box, combo box, etc.) or
command button on the main form, the Exit code triggers.

But many of my controls on the main form are labels with on click code
behind them. (I prefer using labels with on click code, as opposed to
command buttons, because I think labels provide a cleaner look.) But
when a user clicks on one of these labels, the Exit code isn't
triggered. (It's as if the labels aren't really "pressed down" like
command buttons. Moving the code from on click to mouse down doesn't
fix it.)

I had the same problem when I used a label called "Delete" for each
row on a continuous form. No matter what record the user was on,
clicking Delete wouldn't detect the actual record that had the focus.
Changing it from a label to a command button fixed the problem.

How can I get Access to handle labels with on click code the same way
it handles command buttons with the same code?

One possibility for keeping the look of your labels without complicated
workarounds is to put a *transparent* command button over each label,
and use that button's Click event instead of the label's Click event.
Command buttons do have a Transparent property that allows the button to
be clicked and receive the focus, but be invisible.
 

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

Top