preventing unwanted characters

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I have a field where the the user enters a number that is usually hyphenated
(phone number, SSn, etc.)

Is there a way to make sure the number is entered into the table without the
hyphen, or parens or any other non numeric character?
 
So you want a straight number as the data yeah?
In design view make the data type 'Number'.
If the user enters a hyphen etc it will show an error.
Except for comma or dot which it will remove the comma and anything after
the dot unless you change decimal places.
 
I have a field where the the user enters a number that is usually hyphenated
(phone number, SSn, etc.)

Is there a way to make sure the number is entered into the table without the
hyphen, or parens or any other non numeric character?

You can use an Input Mask, e.g. for SSN you could use

"000-00-0000;;"

to store nine digits but display it formatted with hyphens (they won't
actually be stored in the table).

I would NOT recommend using a Number type field - for one thing it won't
support leading zeros, and a Long Integer is limited to values under
2,000,000,000 odd (ok for SSN but too small for lots of other identifiers).
 
Brian,

Rather than allowing the user to enter invalid characters and then
asking them to do it again I normally prevent them from entering
invalid characters in the first place. To do this paste the following
code into the KeyPress event of your text box so that it looks like
this :-

Private Sub YourFieldName_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) Like "[!0-9]" And _
KeyAscii <> vbKeyBack Then KeyAscii = 0
End Sub

Change the word YourFieldName for the name of your text field.

This allows the user to press keys 0 to 9 and the BackSpace
key only.

HTH

Peter Hibbs.
 
I agree with John that number datatype is not a good idea in this case, but
for the record, there are number types that store more than long integer.
Decimal can hold up to 18 digits.
-TedMi
 
I was looking more for consistency, not numeric values. I don't want some
entries with the hyphen and some without - yet do not want to limit the field
size to just 9 or 10 characters
 
Back
Top