Code
for any Text Keypress Event
Masked Text Controls Sample
Function KeyNumeric(KeyAscii As Integer,ctl As Control, DecPlace As Integer) As Integer
' Call this from the keypress event. For example to allow 2 decimal places - KeyAscii = KeyNumeric(KeyAscii, Text1, 2)
Dim intDec As Integer
Dim strtxt As String
strtxt = Trim$(ctl.Text)
intDec = InStr(strtxt, ".")
Select Case KeyAscii
Case 8 ' Backspace
Case 48 To 57 ' Valid Numerics
If intDec > 0 Then
If ctl.SelStart> intDec - 1 Then
If Len(Mid$(strtxt, intDec + 1)) > DecPlace - 1 Then
KeyAscii = 0
Beep
End If
End If
End If
Case 46 ' Decimal Point
If intDec>0 Or DecPlace = 0 Then
KeyAscii = 0
Beep
End If
Case Else ' Invalid characters
KeyAscii = 0
Beep
End Select
KeyNumeric = KeyAscii
End Function
I keep a series of these in my TEXT.BAS file and deploy them as needed...
![]()