Limit typing into text box to digits only

obs

Registered User.
Local time
Today, 07:10
Joined
Mar 1, 2007
Messages
25
Hi.

I have a text box in a form, which is bound to an "ID Number" field in a table. The field is of a text type.

I would like that in the text box in the form, the user be limited to typing only 0-9 digits, and when he tries to type any other character it will not be allowed.

How can this be achieved ?

I would appreciate any help on this.
 
(Leaving aside that Access should prevent any numeric field from being updated with invalid data, such as anything other than a number or that equates to a number.)

You can specify what sort of characters can be entered into a text box by setting the Input Mask property. This is found under the Data tab when viewing the properties of the text box. You can also set this property for the field when designing the table. Access will then import the settings whenever you add the field to a form, saving you having to repeat the work each time the field is add to a form.

If you are unsure about how to set the Input Mask property, put the cursor in the field and then press F1 and the help file should be displayed for you.

HTH

Tim
 
Alternatively, there's an input mask wizard if you click on small button "..." on immediate right of the input mask field.
 
Thanks Banana.

obs,

note that the input mask wizard will only work with Text and Date type fields so if your field is set as numeric you won't be able to use the wizard. You can still use an input mask for a numeric field, but the contents have to be numeric compatible. Basically, if you're using a numeric field you're going to be restricted to using just zero's (or nine's) in your input mask, one for each possible number that you expect in your field. Use zero's for compulsory digits and nine's for optional digits

Tim
 
Place this code in the KeyPress event for your textbox and it will only allow 0-9 to be entered, giving a warning/instructional message if the user enters anything else. Just take out the line:

MsgBox ("You Must Enter Numbers Only!")

if you don't want the messagebox
Code:
Private Sub [I][B]YourTextBoxName[/B][/I]_KeyPress(KeyAscii As Integer)
If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii = 8) Then
      KeyAscii = KeyAscii
      Else:
      MsgBox ("You Must Enter Numbers Only!")
      KeyAscii = 0
   End If
End Sub
The (KeyAscii > 47 And KeyAscii < 58) allows 0-9 to be entered and (KeyAscii = 8) allows the user to use the <BackSpace> key if necessary.

Linq
 

Users who are viewing this thread

Back
Top Bottom