View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Afterupdate problem with setfocus on a txtbox in a form

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.



wrote:

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


--

Dave Peterson