Inputting numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to ensure data entry people can only type in whole numbers into a
numeric field (Number of children). I have set the field to Byte, but data
entry still allows someone to type 1.5, and the number is then rounded. How
can I supply an error message or validation rule to not allow decimals to be
entered?
 
You can add some code to the KeyPress event of the text box so it destroys
any keystroke that is not a digit.

1. Open your form in design view.

2. Right-click the text box, and choose Properties.

3. Set the On KeyPress property to:
[Event Procedure]

4. Click the Build button (...) beside this. Access opens the code window.

5. Between the "Private Sub..." and "End Sub" lines, enter:
Call DigitOnly(KeyAscii)

6. In a standard module (Modules tab of Database window), paste the function
below:

Sub DigitOnly(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If
End Sub


Note: the function also prevents the user from typing a minus sign.
 
On Thu, 14 Oct 2004 10:39:03 -0700, J LaCross <J
I want to ensure data entry people can only type in whole numbers into a
numeric field (Number of children). I have set the field to Byte, but data
entry still allows someone to type 1.5, and the number is then rounded. How
can I supply an error message or validation rule to not allow decimals to be
entered?

Set the Input Mask property of the field to

90

This will require them to type one digit, allow them to type two
digits, and prohibit them from typing any other characters.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
Back
Top