I have a field on a form which is used to enter the email address of a customer.
I have put some code on after update to check if the entered Email address is valid.
If its not I want to set focus on that field and select the text but I dont want the record to save, the email has to be valid to be able to save.
However, the SetFocus just isnt working it doesnt set focus on that field and then select the text.
Heres my code
I commented out the Me.Email = Null because I want the user to see the false entered Email, but as I mentioned before, I dont want the record to be saved.
I have put some code on after update to check if the entered Email address is valid.
If its not I want to set focus on that field and select the text but I dont want the record to save, the email has to be valid to be able to save.
However, the SetFocus just isnt working it doesnt set focus on that field and then select the text.
I tried the DoCmd.CancelEvent but same thing.KeyDown → BeforeUpdate → AfterUpdate → Exit → LostFocus
You can re-set the focus anywhere in there and it will still keep following the pattern. So we need to tell it to stop following the pattern. Replace your Me.MyFld.SetFocus with DoCmd.CancelEvent and it should fix your problem. Basically, this just kicks you out of the above pattern, so the Exit and LostFocus events never fire...
Heres my code
Code:
Private Sub Email_AfterUpdate() '20190513
If InStr(Email, "@") = 0 Or InStr(Email, ".") = 0 Then
MsgBox "E-mail not valid!"
DoCmd.CancelEvent
CheckBox1 = False
With Me.Email
.SelStart = 0
.SelLength = Len(Me.Email)
End With
'Me.Email = Null
Exit Sub
End If
If Not IsNull(Email) Then
If Not IsEmailAddress(Email) Then '20190513
MsgBox "E-mail not valid!"
CheckBox1 = False
DoCmd.CancelEvent
With Me.Email
.SelStart = 0
.SelLength = Len(Me.Email)
End With
'Me.Email = Null
Exit Sub
End If
End If
End Sub
I commented out the Me.Email = Null because I want the user to see the false entered Email, but as I mentioned before, I dont want the record to be saved.