PDA

View Full Version : setfocus on a form



philfer
08-27-2010, 02:27 PM
Hello,

I have a textbox on a userform with the following associated code :-

Private Sub txtAmount_AfterUpdate()

'error handling
On Error GoTo Err_txtAmount_AfterUpdate

'if the user types in an amount covert it to millions with 2dp
txtAmount.Value = Format(CDbl(FormatNumber(txtAmount.Text, 2)), "#,##0.00")

'error handling
Exit_txtAmount_AfterUpdate:
Exit Sub

Err_txtAmount_AfterUpdate:
MsgBox "Please ensure you are typing in a number"
Me.txtAmount.Text = ""
Me.txtAmount.SetFocus
Resume Exit_txtAmount_AfterUpdate

End Sub


but it doesnt set the focus on the control but moves onto to the next one

Does anyone know why not???

Thanks
Phil

Bob Phillips
08-27-2010, 02:53 PM
I recall having a long discussion on this some years ago. The move that triggers the AfterUpdate seems to take precedence over the SetFocus. I don't recall that we worked out a resolution.

Kenneth Hobs
08-27-2010, 04:16 PM
If I understand:
Dim Can As Boolean

Private Sub txtAmount_AfterUpdate()
Can = False
'error handling
On Error GoTo Err_txtAmount_AfterUpdate

'if the user types in an amount covert it to millions with 2dp
txtAmount.Value = Format(CDbl(FormatNumber(txtAmount.Text, 2)), "#,##0.00")

Exit Sub

Err_txtAmount_AfterUpdate:
MsgBox "Please ensure you are typing in a number"
Me.txtAmount.Text = ""
Can = True
End Sub

Private Sub txtAmount_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Can
End Sub

Private Sub CommandButton1_Click()
Unload Me
End Sub