Limiting text data type to numeric input

C

ctdak

Can a text box control with a text data type source be limited so as to only
allow the user to input numeric characters (no alpha or special characters)?
I have need to allow the user to input up to a 14 digit number which is
larger than a long integer data type can handle, so I'm using a text data
type.

ctdak
 
L

Linq Adams via AccessMonster.com

Sure! For the 14 digit limit, since you're utilizing a Text datatype, in the
table just set the length to 14.

To limit input to 0-9, use this code, replacing

FauxNumber

with the actual name of your textbox.

Private Sub FauxNumber_KeyPress(KeyAscii As Integer)

If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii = 8) Then
KeyAscii = KeyAscii
Else:
MsgBox ("You Must Enter Digits 0-9 Only!")
KeyAscii = 0
End If

End Sub
 
L

Linq Adams via AccessMonster.com

Sorry, forgot about allowing for tabbing out of the field, modify that to:

Private Sub FauxNumber_KeyPress(KeyAscii As Integer)

If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii = 8) Or (KeyAscii = 9) Then
KeyAscii = KeyAscii
Else:
MsgBox ("You Must Enter Digits 0-9 Only!")
KeyAscii = 0
End If

End Sub
 
C

ctdak

Linq,

Thanks so much. This is exactly what I needed. I had not used this event
procedure before. One thing I noticed was that tabbing out of the field does
not require the "Or (KeyAscii = 9)". Tabbing out is no problem, but tabbing
into the field does need this added to the test or else the message comes up.

ctdak
 

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