Setfocus to same field

D

Dhonan

I am using VB code in the "after update" event for a field on a form.
The code checks for errors and if there is an error, it is displayed using
Msgbox.
The next step attempts to set focus to the same field so the error can be
corrected (e.g. Me!text1.setfocus). However, it is not setting focus to the
same field, instead going to the next tabbed field. I have also tried the
docmd.setfocus with the same result.

Any ideas? Thank you so much.
 
A

Allen Browne

As you found, SetFocus has no effect. That's because the focus has not left
the control yet. When it does Access will set the focus to the next control
in the tab order, so any SetFocus you add to this event is meaningless.

To keep the focus in the control until a suitable value has been entered,
use its BeforeUpdate event, and cancel the event if you need to stay there.
 
D

Dhonan

Thank you so much - it worked great. One other question - when it stays on
the cotrol the cursor is sitting at the end of the field - is there a way to
"highlight" the entire value in the field so the user doensn't have to back
tab or back space to change it?
 
F

fredg

I am using VB code in the "after update" event for a field on a form.
The code checks for errors and if there is an error, it is displayed using
Msgbox.
The next step attempts to set focus to the same field so the error can be
corrected (e.g. Me!text1.setfocus). However, it is not setting focus to the
same field, instead going to the next tabbed field. I have also tried the
docmd.setfocus with the same result.

Any ideas? Thank you so much.

Why are you using the Control's AfterUpdate event for this. It seems
to me, without knowing more about your database, that you should be
using the BeforeUpdate event. Then you could use the BeforeUpdate's
Cancel = True argument to cancel the update and the focus will remain
where it was.

Private Sub SomeControl_BeforeUpdate(Cancel as Integer)
If MsgBox("Hey guy something's wrong! Shall we fix it?",vbYesNo) =
vbYes Then
Cancel = True
End If
End Sub
 
A

Allen Browne

You could programmatically set SelStart and SelLength so the selection is
all the Text in the control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top