Solved Restricting Entry to Numbers Only in Text Field (1 Viewer)

RogerCooper

Registered User.
Local time
Today, 08:58
Joined
Jul 30, 2014
Messages
286
I have a field that should only have numbers as characters, but it can have a leading zero, so I can't use a numeric field. Is there any way that I can prevent accidental entry of non-numbers in the field at the database level?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:58
Joined
Oct 29, 2018
Messages
21,473
Hmm, at the table level, you could try using the Validation Rule property. Other possible options are the Format and Input Mask properties.
 

RogerCooper

Registered User.
Local time
Today, 08:58
Joined
Jul 30, 2014
Messages
286
This worked in validation

Code:
IsNumeric([Entry#])=True
 

ebs17

Well-known member
Local time
Today, 17:58
Joined
Feb 7, 2020
Messages
1,946
but it can have a leading zero
Is there a rule for this?

One can also store numbers as numbers in a number field and display them formatted:
SQL:
SELECT NumberField, Format(NumberField, "0000") AS NumberFormat FROM TableX
 

RogerCooper

Registered User.
Local time
Today, 08:58
Joined
Jul 30, 2014
Messages
286
The length of the values can vary.

Even if the values had a fixed length, I would be uncomfortable using a numeric field with something that is not really a number. It could cause future problems if somebody thought is was really number and didn't pad with leading zeros.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:58
Joined
May 21, 2018
Messages
8,529
If you like to alert them as soon as they type an illegal character instead of waiting to the end
Code:
Private Sub AlertNonNumeric(ctl As Access.Control)
  If Not IsNumeric(ctl.Text) Then
    MsgBox "Only numeric characters allowed"
      ctl.Value = Left(ctl.Text, Len(ctl.Text) - 1)
      ctl.SelStart = Len(ctl.Text)
  End If
End Sub

Private Sub Text0_Change()
  AlertNonNumeric Text0
End Sub
 

ebs17

Well-known member
Local time
Today, 17:58
Joined
Feb 7, 2020
Messages
1,946
Bad character: Is the decimal delimiter a bad character?
Code:
? IsNumeric("0045.67"),  IsNumeric("0045.")
Wahr          Wahr
 

moke123

AWF VIP
Local time
Today, 11:58
Joined
Jan 11, 2013
Messages
3,920
if decimal is an issue

Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)

    AllowOnlyNumbers KeyAscii, Me.Text0

End Sub


Public Sub AllowOnlyNumbers(ByRef KeyAscii As Integer, ctl As TextBox)


    Select Case KeyAscii

        Case 48 To 57           '0 to 9
            'do nothing
        Case 46                 'comment out if a "." is not needed  (ie. 2.1)
            'do nothing


        Case Else
      
            MsgBox "Only Numbers Permitted"
            ctl.SetFocus
            ctl.SelStart = Len(ctl.Text)
            KeyAscii = 0
    
    End Select
    
End Sub
 

ebs17

Well-known member
Local time
Today, 17:58
Joined
Feb 7, 2020
Messages
1,946
Regular expressions could also be used for processing. The following abbreviated statement deletes all non-digits.
Code:
AnyValue = RegExReplace(AnyValue, "\D", "")
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:58
Joined
Feb 19, 2002
Messages
43,275
If the "number" contains leading zeros, it is NOT a number, it is a code and codes need to be stored as text. For example, the New England zip codes all start with a leading zero. Excel automatically messes this up so be careful with import/export.

Using the BeforeUpdate event of the form is generally the best place to do validation. The IsNumeric() does accept some letters because it considers anything in scientific notation to be numeric. So, at a minimum e and d will get past that function.

Or, I would use the function posted by @MajP
 

sxschech

Registered User.
Local time
Today, 08:58
Joined
Mar 2, 2010
Messages
793
In reference to the code for RegExReplace, it can be found here under post #15:
 

RogerCooper

Registered User.
Local time
Today, 08:58
Joined
Jul 30, 2014
Messages
286
Bad character: Is the decimal delimiter a bad character?
Code:
? IsNumeric("0045.67"),  IsNumeric("0045.")
Wahr          Wahr
A period would be a bad character but in practice that it not likely to be an issue. The problem is inconsistencies in the source document being transcribed, which sometimes has dashes and spaces.
 

Users who are viewing this thread

Top Bottom