Setting Focus to a control

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I posted this question last week, but my post seems to
have gotten lost.

I have a field that receives a number. Using anAfterupdate
event, I ensure the value entered is not null, and is not
less than 0. If it is less than 0, I reset the value in
the field to 0, save the record as is, then issue a
message indicating the message must be greater than 0. I
also want to set the focus back to this field, but it move
to the next control in the form. Why? in setting the
focus, I am using something like the following:

form!fieldName.setfocus

Thanks in advance.

Kevin
 
Kevin said:
I posted this question last week, but my post seems to
have gotten lost.

I have a field that receives a number. Using anAfterupdate
event, I ensure the value entered is not null, and is not
less than 0. If it is less than 0, I reset the value in
the field to 0, save the record as is, then issue a
message indicating the message must be greater than 0. I
also want to set the focus back to this field, but it move
to the next control in the form. Why? in setting the
focus, I am using something like the following:

form!fieldName.setfocus


Just use a Validation Rule property of >=0. It will do all of this for you
with no code.
 
Kevin-

Let's assume the user is pressing Tab to move on. The events fire in this
sequence:
Key Down (Tab key)
BeforeUpdate
AfterUpdate - where your code is running
Exit (this control)
Enter (next control)
KeyPress (Tab key delivered to the next control in the tab sequence)
Key Up (next control)

So, the Tab key is still "in the cue" so to speak. Your code is probably
doing the SetFocus (should use Me.ControlName.Setfocus) OK, but then Access
honors the Tab key.

Why not use the BeforeUpdate event? Why do you want to save the record with
an invalid or zero value? If you test in BeforeUpdate and the value is
wrong, set Cancel = True, and Access will leave the focus on the control
that has the bad value - and won't save the change.

--
John Viescas, author
"Microsoft Office Access 2003 Inside Out"
"Running Microsoft Access 2000"
"SQL Queries for Mere Mortals"
http://www.viescas.com/
(Microsoft Access MVP since 1993)
http://www.deanforamerica.com/site/TR?pg=personal&fr_id=1090&px=1434411
 
Back
Top