Here's what I use. It gives you code for two text fields, one that's limited to alpha characters only, the other to numeric characters only. You could, of course, mix and match if need be, using the appropriate ASCII/Keycode numbers.
Keycode 48-57 represent 0-9
Keycode 190 represents the period/decimal point(.)
Function keys can be referred to by their VB Constants, i.e. vbKeyBack or vbKeyReturn and so forth (Searching in Access' Help for "Keyboard Constants" will get you the whole list)
To make a field only except Alpha character simply set the Keycode value to zero if one of the numeric keys is hit:
Code:
Private Sub AlphasOnlyAllowed_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
KeyCode = 0
End Select
End Sub
To make a field only accept Numerics is a little trickier, because if you only accept Keycodes that represent 0-9, you don’t have the ability to use your backspace key, or your left and right arrow keys, or the return key, and so forth. So to get around this, in a field you’re limited to numbers only, you have to include these function keys as well as Keycode 190, which represents the period or decimal point, assuming you want to allow decimals in your numeric only.
We could, of course, list all 52 Alpha character Keycodes, and set them to zero if they were pressed, but that’s rather cumbersome, so we’ll use the 48-57 plus 190 plus the function keys, and if they’re pressed we’ll leave the Keycode equal to the Keycode. If anything else is pressed, we’ll assign the Keycode, once again, to zero.
Code:
Private Sub NumericsOnlyAllowed_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 190, vbKeySpace, vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft
KeyCode = KeyCode
Case Else
KeyCode = 0
End Select
End Sub
The Missinglinq
Richmond, Virginia
There's ALWAYS more than one way to skin a cat!