Restrict Text Box

A

Abdul Shakeel

Hi All,

I want that in My Access Form in one of my Text box only Numeric values
should enter when user try to enter text in it,......nothing should happen
 
A

Allen Browne

In table design, if you set the Data Type of the field to Number, Access
will reject any entry that is not a valid number.

If you want to block any characters that are not digits (including the
decimal point, negative sign, etc.) you could paste the function below into
a general module, and then set your text box's On KeyPress property to:
[Event Procedure]
and add the code:
Call DigitOnly(KeyAscii)

Sub DigitOnly(KeyAscii As Integer)
On Error GoTo Err_DigitOnly
'Purpose: Disallow any keystroke except 0 ~ 9, and backspace.
'Usage: In a text box's KeyPress event procedure:
' Call DigitOnly(KeyAscii)

If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If

Exit_DigitOnly:
Exit Sub

Err_DigitOnly:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_DigitOnly
End Sub

Note that this code will have the side effect of disabling any non-digit
hotkeys while this text box has focus.
 
Top