View Full Version : setfocus method


iommi
03-10-2000, 11:37 AM
Hello again.

Sorry to disturb again, but i forgot to ask one thing in my question below.

If I put some validation code in the afterupdate or onexit event in a textbox, runs the validation (something is not to be less than 3) via the form (The user types some value which my validation discovers) and I want to first display a msgbox "Wrong value - lower than 3" and the run the [txtbox].setfocus method on the txtbox ...how to do it?

The txtbox just wont get focus; The control AFTER the txtbox will always get the focus!

To put it in other words, I want to loop and loop on the samt txtbox, until the user finally gets it right; puts something > 3. Only after this, is the control after my txtbox aloud to get its focus.

How?

the code:

sub validate()
if [txtbox] < 3 then
msgbox "No, no..."
[txtbox].setfocus 'Wont just work!!
end if
end sub

Axis
03-10-2000, 04:56 PM
I'm not sure if this is a glitch in Access, but I've had the same problem with various other codes using SetFocus. I've gotten around it by using SetFocus on another control first, then going to the control I want:

[any other control].SetFocus
[txtbox].setfocus

Maybe someone else has a better solution, but this does work.

R. Hicks
03-11-2000, 06:02 PM
Can you not use the Before Update event to validate your entry instead of the After Update event? You have the abilty to cancel, and the focus remains in the current txtbox.

Just a thought,
RDH

iommi
03-12-2000, 11:11 PM
No. I tried this after readíng your advice...
but I cannot validate in the beforeupdateevent. Access complains about this. It says its values must be saved before the code in the beforeupdateevent occours (in my case the validation).
So its back to the origin problem. But I followed the advice to have another control to escape to on the afterupdate event...and if myvar = 1 then return to the [firstcontrol].

Not a sexy solution, but it works.

Many thanks for your advice.

BarkerD
03-13-2000, 07:48 AM
Why don't you add a CancelEvent command on the OnExit event if the validation fails. This won't allow the focus to change until the value is acceptable

MikeAngelastro
08-17-2001, 08:58 AM
Hi,
It looks like, in Access 2000, we must now do data validation on the exit event instead of the after update event, which is the way we did it in Access 97.

Mike