setfocus on combobox not working

D

dfeigen115

I have a combox (CACRNumber) that requires an entry be selected in order to
set the values for other controls on the form. In the LostFocus event, I
check to see if the combo box is null and if so display a msgbox and reset
the focus back to the CACRNumber. In this instance, the msgbox displays as
planned, but the focus does not return to the CACRnumber combo.

If IsNull(Me.CACRNumber) Then
MsgBox ("Please select a CACR from the list")
Me.CACRNumber.SetFocus
Else
DoCmd.RunCommand acCmdRefresh
End If

Thanks for your time, Dan
 
T

Terry Kreft

You're using the wrong event.

The beforeupdate event should be used for this, you set the Cancel argument
of the event to true to prevent focus moving from the control.
 
D

dfeigen115

Terry, I moved the code from the lostfocus event to the before update as you
suggested, using the cancel, unfortunately, this worked even less as the
msgbox did not display either. Any other suggestion?
thanks, Dan
 
R

Rick Brandt

dfeigen115 said:
Terry, I moved the code from the lostfocus event to the before update
as you suggested, using the cancel, unfortunately, this worked even
less as the msgbox did not display either. Any other suggestion?
thanks, Dan

BeforeUpdate will only work if they actually make an entry. If they are
simply tabbing through the control without making an entry then BeforeUpdate
of the vcontrol will not catch that. BeforeUpdtae of the Form would though
or you could just make that field required at the table level and then the
record won't save unless they fill it in.

If you insist on catching it at the control you can use the Exit event which
can also be cancelled, but you STILL need the form event or the required
property in your table because they might not ever enter the control in the
first place.
 
D

dfeigen115

Rick, I do need the capture it (the null selection) at the control level, as
the selected entry dirves what other controls are displayed and/or
autopopulated. I tried placing the code in both the form and control "before
update" but as you noted, if the just tab or click to another field the
ommission is not caught.
Dan
 
B

BruceM

You could set the focus to the combo box in the form's Current event, then
use its Exit event to generate the message box as needed. I don't know
exactly what your combo box does, but in any case I expect its After Update
event would run some code, so there is no need for an Else part of the If
statement in the Exit event. If a selection is made in the combo box the
Before Update event will run. The Exit event fires after the Before Update
event, so making a selection in the combo box will prevent the message box
from appearing.
The form's Before Update event runs when you save the record, either by
attempting to navigate away from it or by explicitly saving the record.
Remember, though, that any Before Update event, whether at the control level
or the form level, will run only if there has been a change.
 
Top