Click to See Complete Forum and Search --> : textbox Keypress
Moses420ca
October 23rd, 2001, 01:20 PM
How do I force a textbox to only accept numbers as a input and not text?
http://www.geocities.com/moses420ca/images/thumbnails/drewblue2.jpgThankz, Drew
Moses420ca@yahoo.ca?SUBJECT=I Love Your Stuff
DSJ
October 23rd, 2001, 01:48 PM
private Sub Text1_KeyPress(KeyAscii as Integer)
If KeyAscii < 47 Or KeyAscii > 57 then
KeyAscii = 0
End If
End Sub
Boumxyz2
October 23rd, 2001, 02:32 PM
Well the other suggestion works but if you want to block all key except numbers and Backspace here is the code
' ***************************************************************
' * *
' * Sub-routine name : *
' * TxtFTE_Keydown *
' * *
' * Objective : *
' * Checks which key was pressed *
' * *
' * Parameters : *
' * Keycode : The key that was pressed *
' * ****: Check wether shift was pressed*
' * or not. *
' * *
' ***************************************************************
private Sub txtFTE_KeyDown(KeyCode as Integer, Shift as Integer)
KeyCode = ValidateKeyNumbers(KeyCode)
End Sub
' ***************************************************************
' * *
' * Sub-routine name : *
' * ValidateKeyNumbers *
' * *
' * Objective : *
' * Blocks all key except backspace and *
' * numbers *
' * *
' * Parameters : *
' * Keycode : The key that was pressed *
' * *
' ***************************************************************
private Function ValidateKeyNumbers(byval KeyCode as Integer) as Integer
If (KeyCode >= vbKey0 And KeyCode <= vbKey9) Or KeyCode = vbKeyBack Or _
(KeyCode >= vbKeyNumpad0 And KeyCode <= vbKeyNumpad9) Or _
(KeyCode = vbKeyDecimal) or 190 then ' 190 is the dot key
ValidateKeyNumbers = KeyCode
else
ValidateKeyNumbers = 0
End If
End Function
cloud311
October 23rd, 2001, 08:48 PM
You could also try this. Mind you that this doesn't include the "." key, but it is fairly small and works.
private Sub Text1_KeyPress(KeyAscii as Integer)
If Not IsNumeric(Chr(KeyAscii)) then KeyAscii = 0
End Sub
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.