Number field has a mind of its own

K

Kamran

In Word 2003, I've created a form field that I want users to enter a phone
number into. I'm using the format 000-000-0000, set the length to 12, Number
Type numeric, and the output upon leaving the field looks like
"7032227777-0000-".
That's 16 characters -- how can that be??

I'm using other number fields above it in the form but I don't see how they
could affect this one. Any help would be appreciated.
 
K

Kamran

Well, I figured it out. The right format is ###-###-####.
One thing it doesn't seem to be able to do though, is require a minimum
amount of characters. For example, you can type 8 numbers into the field and
it accepts it.
 
K

Kamran

Thanks, Graham. I never would have guessed combining ' and " in the same
mask. I looked over the info on the link you sent, but I couldn't find
exactly which part related to this (I looked at the Form Fields section).
Could you explain how the apostrophes and quote marks are doing their work in
this case?
Thanks again.
 
K

Kamran

Sorry, it doesn't seem to be working. I get results like this:

If I enter 9 digits, like 703444222 I get: "070'-'344'-'4222"
It gives me a leading zero and it displays all the quotes and apostrophes.

If I enter 10 digits, like 7034442222 I get: "703'-'444'-'2222"
again with all the quotes and apostrophes.

I have tried all combinations of removing and adding quotes and apostrophes,
but they always display in the result. Once I remove them completely, I end
up with the original problem. This seems like such a simple formatting
issue......help!
 
G

Graham Mayor

I hadn't realised that this was a form field when I posted my reply. I had
assumed a numeric Word or merge field.

I posted the following in response to a similar query a while back - no-one
did come back with the simpler version that worked.

"Someone is going to come up with a ridiculously simple approach that works,
but in the meantime you can do it with two macros. The first run on exit
from the telephone number form field (here Text1) and the second run on
entry to the following field. The bulk of the macros is concerned with error
trapping. The macros should allow phone numbers to be entered as 1234567890
or (123)-456-7890 but most other formats should be trapped."

I have modified the macros originally posted to eliminate a minor error and
to improve the error trapping. The result is as follows:
Set the form field (here "Text1") to be a text field with unlimited length.

The original macr0 used the format (000)-000-0000 You don't appear to want
the brackets, which makes the macro a little simpler so:

Sub FormatNum()
'run on exit from the phone mumber field
Dim sPhone As String
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) = 10 Then
If IsNumeric(sNum) = False Then GoTo WrongNo
End If
If Len(sNum) <> 10 Then
If Len(sNum) <> 12 Then GoTo WrongNo
If InStr(1, sNum, "-") <> 4 Then GoTo WrongNo
If InStr(5, sNum, "-") <> 8 Then GoTo WrongNo
End If
sPhone = format(sNum, "000-000-0000")
ActiveDocument.FormFields("Text1").Result = sPhone
Exit Sub
WrongNo:
MsgBox "Incorrect telephone number format." & vbCr & _
"Insert 10 digits without spaces" & vbCr & _
"For 123-456-7890 enter 1234567890", vbCritical, _
"Number entry error"
End Sub

Sub FormatError()
'run on entry to the field after the phone number field
Dim sNum As String
sNum = ActiveDocument.FormFields("Text1").Result
If Len(sNum) <> 12 _
Or InStr(1, sNum, "-") <> 4 _
Or InStr(5, sNum, "-") <> 8 Then
ActiveDocument.FormFields("Text1").Select
End If
End Sub


http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Incidentally, your original premise of using ###-###-#### with a ten
character numeric field can also be validated with a macro on exit eg

Sub ValidateText1()
Dim ofld As FormFields
Dim sText As String
Set ofld = ActiveDocument.FormFields
sText = ofld("Text1").Result
If Len(sText) < 12 Then
MsgBox "Incorrect telephone number format." & vbCr & _
"Insert 10 digits without spaces" & vbCr & _
"For 123-456-7890 enter 1234567890", vbCritical, _
"Number entry error"
ofld("Text1").Select
End If
End Sub

As with the other macro, the main problem with macros associated with forms
is getting people who use the forms to allow the macros to rum.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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