I need some help with some data validation. I have a form that populates with data from a worksheet. Part of this data is a phone number. I've tried the code below, but I can't seem to get it to work.
What I want:
If the value in txt_Phone is numeric and 10 digits, the value gets formatted as (###)###-####.
If the value is 13 characters long, the sub exits, as that would be the phone number that loaded into the form.
Any other condition would present the msg box.
What I'm getting:
I can type in a 10 digit number that converts, but I get the msg box when I click the command button that evaluates everything.
If I leave the value that loaded into the text box (formatted as (###)###-####), I still get the msg box.
Can anyone offer some advice on this please?
What I want:
If the value in txt_Phone is numeric and 10 digits, the value gets formatted as (###)###-####.
If the value is 13 characters long, the sub exits, as that would be the phone number that loaded into the form.
Any other condition would present the msg box.
What I'm getting:
I can type in a 10 digit number that converts, but I get the msg box when I click the command button that evaluates everything.
If I leave the value that loaded into the text box (formatted as (###)###-####), I still get the msg box.
Code:
If IsNumeric(Me.txt_Phone.Value) And Len(Me.txt_Phone.Value) = 10 Then Me.txt_Phone = Format(Me.txt_Phone.Value, "(###)###-####")
If Len(Me.txt_Phone.Value) = 13 Then Exit Sub
Else
MsgBox "Please enter a valid phone number for the Client, using numbers only."
If response = vbOK Then Me.txt_Phone.SetFocus
Exit Sub
End If
Can anyone offer some advice on this please?