Allow only whole numbers be entered in text box

G

Guest

I'm thinking this should be a simple issue... How do I get it so a User is
only allowed to enter a whole number in a text box? I have the field
formatted as Fixed with 0 decimals, which does round it to the nearest whole
number, but I would like to have an error or MsgBox display if the value
entered contains a decimal.

Thanks!
 
A

Allen Browne

You could use the KeyPress event of the text box to see if the KeyCode is
the decimal point. If so, you can destroy the keystroke by setting it to
zero, i.e.:
If KeyCode = 46 Then KeyCode = 0
 
G

Guest

In the Before Update event of the text box:

If Me.MyTextBox - Int(Me.MyTextBox) <> 0 Then
MsgBox "No decimals allowed"
Cancel = True
End If
 
A

Al Campagna

Pat,
Several ways to do that. Use the method that you find
You could use an input mask to not allow decimal values (beeps but does not msg)
or
Code the following on AfterUpdate...
If SomeField <> Int(SomeField) Then
MsgBox "Integers Only"
SomeField = Int(SomeField) 'just corrects it without any message
End
or
Validation Rule: Int([SomeField]) = [SomeField] \
Validation Text: Integers Only

I would tend to use method 2, because there is no operator intervention (no click OK to
Msgbox)
 
G

Guest

In the field InputMask property you can write

####################

As long that you need, that wont allow the user to enter any chr rather then
number
 

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