Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate Access 2010 form text box, integers only (0-9) 1

Status
Not open for further replies.

Webkins

Programmer
Dec 11, 2008
118
US
In Access 2010 I have a form with many text boxes. I want to validate the data before saving it. I want only integers (0-9) in this box, anything other than (0-9) will bring up a error msgbox. The data will always be a 12 digit part number. I am having a hard time and have been searching for some time now on how to do this. Any help or suggestions will be greatly appreciated. Thank you.
 
In the BeforeUpdate event procedure:
Code:
If Me![your P/N control] & "" Not Like "############" Then
  MsgBox "Invalid part number"
  Cancel = True
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another approach, albeit a little more complicated, simply prevents the user from entering anything except Digits:

Code:
Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)

 Select Case KeyCode
  
  Case 48 To 57
   'Numerical characters are allowed
  
  Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab
   'Allow these keys to be used
  
  Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9
   'Allow input from Numbers Keypad

  Case Else
   'Don't allow any other keys to be used
   KeyCode = 0
 End Select

End Sub

The Missinglinq

Richmond, Virginia

The Devil's in the Details!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top