TextBox.Value Format

E

Elaine

Hi,

I have a textbox (txtPhone) that I want to have the end user enter the phone
number into. What I want to do is have the 10 numbers display back to the
user like: ###-###-#### or (###) ###-#### or ###.###.#### (which I can do,
no problem) ... My problem is, if the user goes back to modify the number on
the form before they hit "continue" I can NOT get the 10 numbers to be
stored again by themselves. If I do myphone = Val(txt.Phone.value) myphone =
"0" ... how do I get the txtPhone.Value to be stored as ########## and
display as something else (similar to a mask).

Here is my code:

If Not IsNumeric(txtPhone.Text) Then
If txtPhone.Text = "" Then
Exit Sub
Else
MsgBox ("Please type numbers only")
txtPhone.Text = ""
cancel = True
End If
ElseIf txtPhone.Text = "" Then
'txtPhone = "###-###-####" Then
txtPhone.Text = ""
ElseIf txtPhone.TextLength <> 10 Then
MsgBox ("Phone Numbers should be 10 digits")
cancel = True
Else
txtPhone.Text = Right(Format(txtPhone.Text, "(###) ### ####"), 15)
End If

Thanks,

Elaine
 
B

Bear

Elaine:

You could think of processing the text -- whatever it is -- into a numeric
string from the outset, and then evaluating that string and then formatting
it and spitting it back out if it's valid.

I used this code on a selection, but you could adapt it to work on your
textbox text. You'd build a variable from the textbox text for internal use.
The textbox text is just to gather and display.

Dim strResult As String
Dim I As Integer

strResult = ""

For I = 1 To Selection.Characters.Count
If IsNumeric(Selection.Characters(I)) Then
strResult = strResult & Selection.Characters(I)
End If
Next I

This extracts all the numbers from the text into a single string. Now you
can determine whether that string is ten characters long, etc. You can also
now allow the user to enter nonnumeric characters. Your only invalid entry is
when there are not exactly ten digits in what they enter.

Bear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top