VB Snippet - Force numeric entry (allow decimal)
I had issues with allow user to enter numbers with decimals into textboxes. This is a simple way to allow only numeric values, while allowing only one decimal to be entered...
VB Code:
' USAGE
' In Keypress event of Textbox
ForceNumeric(KeyAscii,Me.ActiveControl)
Function ForceNumeric(KeyAscii, CONTROL As CONTROL)
Dim intKeyAscii As Integer
' MAKE SURE IT'S A DIGIT
intKeyAscii = 0
If KeyAscii >= Asc(0) And KeyAscii <= Asc(9) Then intKeyAscii = KeyAscii
If KeyAscii = 8 Then intKeyAscii = 8
' THIS ALLOWS A DECIMAL TO BE ENTERED...
' IF ONE NOT ENTERED ALREADY
If KeyAscii = 46 And InStr(CONTROL, ".") Then
KeyAscii = 0
ElseIf KeyAscii = 46 Then
intKeyAscii = 46
End If
KeyAscii = intKeyAscii
End Function