Afterupdate problem with setfocus on a txtbox in a form

J

jamesp

I'm using after update to check to see if a date function is correct. If I
click away inside another txtbox (say txtB) and the test on the previous
txtbox (txtA) failed the txtA.setfocus doesn't work (if you step through it
sets the focus back to txtA but then goes to txtB when the macro finishes)

Private Sub txtA_AfterUpdate()
'test to see if correct date
If checkDate(txtA.value) = false Then
Msgbox "Wrong date"
txtA.SetFocus
End If
End Sub

Any ideas?

Many thanks

James
 
D

Dave Peterson

Instead of using _afterUpdate, try using _Exit.

It has a cancel parm that will keep the user in the textbox.

Private Sub txtA__Exit(ByVal Cancel As MSForms.ReturnBoolean)

'test to see if correct date
If checkDate(txtA.value) = false Then
Msgbox "Wrong date"
cancel = true
End If
End Sub

As a user, I'd rather see a label in the userform (big red letters) that gets
that warning/error message than having to dismiss a msgbox.

And if you have a cancel button on your userform, you'll want to change the
..takefocusonclick property to false. Then the user can click cancel even if the
txtA value isn't a date.
 

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

Top