Numbers and "." allowed in textbox only??

CornMaster
08-28-2000, 09:03 PM
Does anyone know how to force a text box to accept only numbers and the decimal? I found a module that would allow only numbers but I need the decimals too.
Can anyone help me?!?!?!?!

CornEmpire Board Administrator
<A HREF="http://www.geocities.com/thecornmaster/" target="_new">http://www.geocities.com/thecornmaster/</A>

d.paulson
08-28-2000, 10:31 PM
Put this in a module

Public Function OnlyNumericKeys(KeyAscii As Integer) As Integer
Select Case KeyAscii
Case 8, 45, 46, 48 To 57 'allow backspace,"-",".", and digits
Case Else: KeyAscii = 0
End Select
OnlyNumericKeys = KeyAscii
End Function

and call it like this

Private Sub text1_KeyPress(KeyAscii As Integer)
KeyAscii = OnlyNumericKeys(KeyAscii)
End Sub



d. paulson

CornMaster
08-28-2000, 11:45 PM
Thank You d.paulson
You are my saviour. It worked perfectly. I only wish I better understood what it did. I often draw a blank when I try to understand others code. (Although I guess people do the same when they look at mine. /images/icons/wink.gif)

CornEmpire Board Administrator
<A HREF="http://www.geocities.com/thecornmaster/" target="_new">http://www.geocities.com/thecornmaster/</A>

Moshky
08-30-2000, 06:37 PM
Here is another solution that won't let you type more than one dot or dash for negative numbers.

Put this in a module:

Public Function ValidateNumeric(strText As String) As Boolean

If Asc(strText) = vbKeyReturn Or Asc(strText) = vbKeyBack Then ValidateNumeric = True: Exit Function
ValidateNumeric = CBool(strText = "" _
Or strText = "-" _
Or strText = "-." _
Or strText = "." _
Or IsNumeric(strText))
End Function

Then put this on text KeyPress Event:

If InStr(Text1.Text, ".") And KeyAscii = Asc(".") Then KeyAscii = 0: Beep: Exit Sub
If InStr(Text1.Text, "-") And KeyAscii = Asc("-") Then KeyAscii = 0: Beep: Exit Sub

If Not ValidateNumeric(Chr$(KeyAscii)) Then KeyAscii = 0: Beep

Hope it help



Lār***

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum