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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a character on KeyPress event

Status
Not open for further replies.

wilky27

Programmer
Aug 22, 2003
9
0
0
GB
Hello,

I have created a text box which will only allow numeric characters to be entered including decimals, but crashes when you start the number with "." instead of "0." So I need a KeyPress event which will convert the two. I've started with this but can't get it to work properly:

If Left(txtMobileVATPer.Text, 1) = "." Then
txtMobileVATPer = "0."
End If

Does anybody have any other suggestions.

Thank You.

Wilky.
 
Ah huh! You can type in negative numbers too ie: -34

Private Sub Text1_Change()
If Not ValidateNumeric(Text1.Text) Then
Text1 = Left(Text1.Text, Len(Text1) - 1)
Text1.SelStart = Len(Text1)
End If
End Sub

Private Function ValidateNumeric(strText As String) _
As Boolean
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function
 
Thanks mate, been a great help!!

Merry Xmas!
 
Easier, and international friendly (different decimal and thousand separators), would be to just use:

IsNumeric(Text1.Text & "0")

in the change event, and then

IsNumeric(Text1.Text) in the validate event...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top