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!

Valiate entry text is only numbers in VBA 1

Status
Not open for further replies.

Webkins

Programmer
Dec 11, 2008
118
US
I am trying to validate that a text entry box contains only numbers before proceeding with tha VBA code. Any ideas?

Thanks
 

Or you could simply prevent the user from entering anything except 0-9:

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

Select Case KeyCode
'Numerical characters are allowed

Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 104

Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab

[b]Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9[/b]

'Non-numerical characters are not allowed

Case Else
KeyCode = 0
End Select

End Sub
The bolded Case allows the Number Pad Keys to be used; you can omit this if appropriate.



The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I think you will find that setting the format of a control to a numeric format will prevent entry of anything that is not numeric, but will not rule out negative numbers.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top