ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Afterupdate problem with setfocus on a txtbox in a form (https://www.excelbanter.com/excel-programming/423539-afterupdate-problem-setfocus-txtbox-form.html)

[email protected]

Afterupdate problem with setfocus on a txtbox in a form
 
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

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

James Price at Premier

Afterupdate problem with setfocus on a txtbox in a form
 
That's great - thanks Dave and have a good weekend

James

"Dave Peterson" wrote:

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



All times are GMT +1. The time now is 11:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com