> Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full
stops or backspace.
Im sure this is really simple, and i will be greatful for a respons..
Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If
End Sub
There is a problem with trying to do what you asked in the KeyPress
event only... the user will be able to Paste bad data into the TextBox.
Here are some solutions which I've posted in the past (there is code
below for both, entries with digits only and for entries with decimal
points). The routines work quite well and protects the TextBox from
pasting non-numeric entries (the user can paste valid data though) as
well as stopping non-numeric keypresses.
Rick - MVP
For typing digits only in the TextBox
=====================================
Dim LastPosition As Long
Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With Text1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub
Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
For typing floating point numbers in the TextBox
=========================================
' Set the maximum number of digits before the
' decimal point in the MaxWhole constant. Set
' the maximum number of digits after the decimal
' point in the MaxDecimal constant.
Dim LastPosition As Long
Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 4
Const MaxWhole As Integer = 2
With Text1
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub
Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
Note that you will have check for the Text property containing a single
character consisting of a decimal point since that must be allowed as a
starting character. If you want to allow negative, as well as positive
values, then use this If statement in place of the second If statement
in the Text1_Change event code above:
If .Text Like "*[!0-9.+-]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*" & String$(MaxWhole, "#") & "[!.]" Or _
.Text Like "?*[+-]*" Then
Note that now you will have to check the Text property for this one to
see if it contains a single plus, minus or decimal point.
I guess I should mention that I'm in the US where the decimal point is a
"dot". If your decimal point is some other characters, then make the
obvious substitutions in the If-Then tests above; or you could query the
system for the decimal point character, store it in a variable and
concatenate that into the string values above in place of the decimal
point ("dot") that I show above. In keeping with the non-APIness of this
solution, here is what I use to get the system's decimal point.
DecimalPointSymbol = Format$(0, ".")