Here’s an easy method for making sure your users enter only numeric data, and only one decimal point. First, place two Public procedures in a standard module. You can use Private procedures in a form if you’re only using it there, but you’ll lose easy portability for future projects.
The first procedure makes sure the decimal point is only entered once. The second procedure filters out all non-numeric characters except the decimal point:
Public Sub DecCheck(Target As String, ByRef KeyStroke As _ Integer) If InStr(Target, ".") And KeyStroke = 46 Then KeyStroke = 0 End IfEnd SubPublic Sub NumCheck(ByRef KeyStroke As Integer) If (KeyStroke < 48 Or KeyStroke > 57) And (KeyStroke _ <> 46 And KeyStroke <> 8) Then KeyStroke = 0 End IfEnd Sub
Then invoke the code from your TextBox’s KeyPress event:
Private Sub txtUnitPrice_KeyPress(KeyAscii As Integer) DecCheck txtUnitPrice, KeyAscii NumCheck KeyAsciiEnd Sub
One caveat: This code doesn’t prevent text characters from being pasted in via the clipboard.