I am using a Textbox for data input. I would like the textbox to accept only numeric data, is there some parameter/attribute that I can set for this?
Printable View
I am using a Textbox for data input. I would like the textbox to accept only numeric data, is there some parameter/attribute that I can set for this?
Set the Tag Property of any Textbox you want to be Numeric Only to N and use this:
private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (byval hwnd as Long, byval nIndex as Long) as Long
private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long
private Const GWL_STYLE = (-16)
private Const ES_NUMBER = &H2000
private Sub Form_Load()
Dim oCTRL as Control
for Each oCTRL In me
If TypeOf oCTRL is TextBox then
If UCase(oCTRL.Tag) = "N" then
'Make the Textbox Numeric Only
Call SetWindowLong(oCTRL.hwnd, GWL_STYLE, _
GetWindowLong(oCTRL.hwnd, GWL_STYLE) _
Or ES_NUMBER)
End If
End If
next
End Sub
Aaron Young
Analyst Programmer
[email protected]
[email protected]
Or you can use this much simpler code
1) Make a textbox (text1) and set it's TEXT property to "" (nothing) so you can see the result
private Sub Text1_Change()
If Not IsNumeric(Text1) then MsgBox "Not a number"
End Sub
Good luck!
Private Sub Text2_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57:
' Allow digits
'enter code for what should
'be done if numeric
Case Else
KeyAscii = 0
Beep
End Select
End Sub
refer ascii chart for the equivalent codes