SetFocus not working

hzeigler4

Registered User.
Local time
Yesterday, 22:41
Joined
Apr 18, 2007
Messages
42
I am trying to require two fields on a subform. One is Description. Here is the code I have in the lost focus event:

If IsNull(Description) Then
MsgBox "Description Required", vbOKOnly + 64, "Description"
DoCmd.CancelEvent
Me.Description.SetFocus
End If

However it does not set the focus back to the description field. Why is this???
 
Does it return an error?

Where does the code execute?
 
The code is in the Lost focus event. It does not return an error, it simply moves to the next textbox on the form instead of focusing back to the description textbox.
 
Does the event even fires?
 
Yes it does. Here is the database. Click Enter Employee Hours. I am trying to make the description and hours worked fields required. If the user leaves them blank it send focus back to textbox. Use Randy Jackson as the employee for testing and use the date 4/21/07
 

Attachments

I can't figure why it wouldn't work.

My suggestion is to put data validation in BeforeUpdate of the form; this is the best place to do it anyway; centralizing data validation makes for a easy to maintain code.
 
Banana - question? If the user simply tabs over the description field without typing in anything the before update event is never fired. That is why I used the lost focus. I can't believe I am struggling so much with this. Any other ideas?
 
To clarify:

Are you using a control's BeforeUpdate event?

If so, you should be using form's BeforeUpdate event. This would mean it wouldn't matter a damn what user did with the record; if it's dirty, and the description is blank, BeforeUpdate will catch the description.

But if you already have records that has blank descriptions and want to catch users when they're simply scrolling through, then you'd need to use OnCurrent event.

Also, be sure to take a look at Ghudson's Better Mousetrap; it's very effective for preventing users from leaving a record unfinished.

HTH.
 
I've run into this before. The problem is that despite the fact that the event is called LostFocus, Access apprently still considers the control to have focus, and so it moves on to the next field. The secret is to move focus to any other control then back to the offending textbox.

Code:
Private Sub OffendingTextBox_LostFocus()
If IsNull(OffendingTextBox) Then
 AnyOtherControl.SetFocus
 OffendingTextBox.SetFocus
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom