validation rule on a text box

  • Thread starter Thread starter David
  • Start date Start date
D

David

I want to make a text box that allows users to input
serial numbers. But I don't want them to type in the
symbol "-" (a.k.a. dash) which sometimes is part of a
serial number format (example: 1234-567) I want it to show
as "1234567" instead.

How can I setup the text box so if a user types in a dash
symbol it either gets wiped out when text box no longer
has focus, or use an error message to user to let them
know that the box won't except their input if they use the
dash symbol.

I assume at this point it would be some sort of validation
rule setup but there may be other answers. Please advise...
 
Try this on the "ON KEY PRESS" event of the field.

If KeyAscii = 45 Then
MsgBox "Dashes are not allowed."
SendKeys "{BS}"
End If

45 is the ascii code for the dash character.
The sendkeys statement will send cause a backspace and
delete the dash that was entered.

Hope this helps.
 
Ashby said:
Try this on the "ON KEY PRESS" event of the field.

If KeyAscii = 45 Then
MsgBox "Dashes are not allowed."
SendKeys "{BS}"
End If
45 is the ascii code for the dash character.
The sendkeys statement will send cause a backspace and
delete the dash that was entered.

Hope this helps.

If you simply want to discard dashes, in the KeyPress event use:

If KeyAscii = 45 Then KeyAscii = 0

If you only want digits in the text box, Put 0 in the Input Mask

HTH
Randy
 
Back
Top