Will not gotocontrol or set focus.

  • Thread starter Thread starter James
  • Start date Start date
J

James

I can't seem to get the following to work when I ad
this either line after the msgbox. It works on the other forms that I have.
Do you know why by chance:

Me.txtCreditCardNumber.SetFocus
or
DoCmd.GoToControl "txtCreditCardNumber"
 
Is the problem that the control doesn't get the focus or that you want it to highlight the
bad number so that the user can start typing to replace it without having to clear it
first?

Also, you may be able to automate this by doing this test in the BeforeUpdate event of
txtCreditCardNumber, saving the user from having to remember to click the Verify button
and without the control losing the focus. You would Cancel the update, pop-up the message
box, and highlight the number or Undo the control so that the number can be retyped.
 
I just tried this on a new, blank record (there was nothing in the textbox).

Private Sub txtField1_BeforeUpdate(Cancel As Integer)
If Me.txtField1.Text <> 11 Then
Cancel = True
MsgBox "Try Again"
End If
End Sub

Note, I'm using the Text value of the textbox because the Value value isn't set until the
update is done. This prevented the textbox from losing the focus, so no need to set the
focus back to the text box. If you also wish to highlight what is in the textbox so that
it will be easier to replace:

Private Sub txtField1_BeforeUpdate(Cancel As Integer)
If Me.txtField1.Text <> 11 Then
Cancel = True
MsgBox "Try Again"
Me.txtField1.SelStart = 0
Me.txtField1.SelLength = Len(Me.txtField1.Text)
End If
End Sub
 
Thank you that is what I was looking for.


Wayne Morgan said:
I just tried this on a new, blank record (there was nothing in the textbox).

Private Sub txtField1_BeforeUpdate(Cancel As Integer)
If Me.txtField1.Text <> 11 Then
Cancel = True
MsgBox "Try Again"
End If
End Sub

Note, I'm using the Text value of the textbox because the Value value isn't set until the
update is done. This prevented the textbox from losing the focus, so no need to set the
focus back to the text box. If you also wish to highlight what is in the textbox so that
it will be easier to replace:

Private Sub txtField1_BeforeUpdate(Cancel As Integer)
If Me.txtField1.Text <> 11 Then
Cancel = True
MsgBox "Try Again"
Me.txtField1.SelStart = 0
Me.txtField1.SelLength = Len(Me.txtField1.Text)
End If
End Sub
 
Back
Top