numeric controll

  • Thread starter Thread starter nicola
  • Start date Start date
N

nicola

How I can make an unbond control accept only nimeric data dose mater wich
data is entered--
Nicola Canada
 
in the KeyUp event of the edit box:

Private Sub MyEditBox_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 Then
MyEditBox.text = Mid(MyEditBox.text, 1, Len(MyEditBox.text) - 1)
MyEditBox.SelLength = 1
MyEditBox.SelStart = Len(MyEditBox.text)
End If
End Sub
 
or using SelectCase, as

Private Sub NumberTextbox_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 48 To 57, 8 To 9, 46
KeyAscii = KeyAscii
Case Else
KeyAscii = 0
End Select

End Sub

it's perhaps a little easier for a person to read the ranges in a
multi-range situation like this, but has no other advantage over an If
statement, AFAIK.

hth
 
or use the Like command :-

Private Sub NumberTextbox_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) Like "[!0-9.]" And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End Sub

Omit the . character if you don't need decimal points, add a -
character AFTER the . if you need negative numbers. The code between
the Private and End Sub lines should all be on one line.

HTH

Peter Hibbs.
 
Back
Top