setfocus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I'm having trouble in setting the focus of my txtbox. I included the
following codes in the lostfocus event.

If IsNull(Me.txtCat2) Or Me.txtCat2.Value = "" Then
MsgBox "Please select an item on the list", vbOKOnly, "Required Data"
txtCat2.SetFocus
End If

However, when the event is triggered, the focus is set to the next txtbox
based on my tab order. Please help.
 
At the point you execute the code, the focus has not left the text box.
Therefore setting focus to it has no effect, and after the event completes
Access moves focus to the next control as usual.

Use the BeforeUpdate event of the control instead of LostFocus. If the entry
is not satisfactory, you can prevent the user from moving on by cancelling
the event, i.e.:
Cancel = True

If you are not happy about the fact that BeforeUpdate fires only if the user
enters something, use the Exit event of the control instead. Unlike the
LostFocus event, Exit can be cancelled. However, this still does not
guarantee that the user entered something, because they may never visit the
control, and so none of its event will fire.

The only way to be sure that a value was entered is to use the BeforeUpdate
event of the *form*. This event fires just before Access is about to write
the record to the table, so you can check all the relevant controls in that
event.

(Of course, you could also open the table in design view, and set the
Required property of the field to Yes. That stops the record being saved if
the field is null.)
 
before your txtcat2.focus

you could try
txtcatxyz.focus

txtcatxyz is the next field on your form.

In other words move the focus from the present field to the next field then
back again.
Allan Murphy
Email: (e-mail address removed)
 
I agree with Allan,
It's a No 8 solution to a common problem, clunky but it works.
I sometimes create a "DummyFocus" text field in my forms, set the size
to zero zero, no tab, and tuck it in some obscure place on the form.
In code I will pass the focus into/out of the [DummyFocus] on the way to
the target control.

Sacrilege to the VBA Adherents, but what the hell - it works!

WSF
 
Back
Top