Two things I'm trying to do:
Limit input to digits 0-9 and
on the 2nd number, add a hyphen so the user won't have to type.
My hopefully end result would be 34-123.
Any help would be appreciated.
Printable View
Two things I'm trying to do:
Limit input to digits 0-9 and
on the 2nd number, add a hyphen so the user won't have to type.
My hopefully end result would be 34-123.
Any help would be appreciated.
No ProblemCode:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
ElseIf Len(Text1.Text) = 2 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = 3
End If
End Sub
Try this.
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
If Len(Text1) = 2 Then Text1.SelText = "-"
End Sub
Thanks guys. It works just like I needed.
Sorry to be a pain but what if I the backspace key to be active? Better yet, is there a chart of KEYASCII codes so I can know what's what?
the backspace key is 8
so enclose everything in the Keypress event in an If Not (KeyAscii = 8) Then ... Endif block.
If you want to get different keyasciis the easiest way is to add another textbox to your form and add this
then run your program , put the curser in the textbox and press the key you want, the correct code will apear in the textboxCode:Private Sub Text2_KeyPress(KeyAscii As Integer)
Text2.Text = KeyAscii
KeyAscii=0
End Sub
James Stanich,
If you are looking for a chart,
MSDN has topics under Character Sets. It lists the charcters along with their charcter codes.
Neat trick. Thanks.
Or even better; use the constant values:
And so on...
- vbKeyBack
- vbKeyReturn
- vbKeyEscape
- vbKeyA
- vbKeyB
- ...
- vbKeyF1
- vbKeyF2
- ...