Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RETURN KEY - KEYASCII 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I have a textbox with a keypress event. All Ascii characters are sent to a label caption, they are all working as expected, however the Ascii code 13 does not get sent to the label, it does when the focus returns to the textbox. I tried making it send its Ascii code when the textbox lost focus, but nothing. What am I doing wrong? Thanks
 
Found why. If there are other fields to goto (which I since added), CHR$(13) does not get passed to my label. I want to control what/where the program should do if the return key is clicked in the textbox, whats the magic answer? Thanks
 
Code:
Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then 
 'Code goes here to do your thing
End If
End Sub

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Many thanks, that works. I am looking at al characters being entered in a field, banning unwanted characters. I have been using Keypress event, should I have been using KeyDown? Thanks aagain
 
Here's what I use. It gives you code for two text fields, one that's limited to alpha characters only, the other to numeric characters only. You could, of course, mix and match if need be, using the appropriate ASCII/Keycode numbers.

Keycode 48-57 represent 0-9
Keycode 190 represents the period/decimal point(.)
Function keys can be referred to by their VB Constants, i.e. vbKeyBack or vbKeyReturn and so forth (Searching in Access' Help for "Keyboard Constants" will get you the whole list)

To make a field only except Alpha character simply set the Keycode value to zero if one of the numeric keys is hit:

Code:
Private Sub AlphasOnlyAllowed_KeyDown(KeyCode As Integer, Shift As Integer)
  Select Case KeyCode
    Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
       KeyCode = 0
    End Select
End Sub

To make a field only accept Numerics is a little trickier, because if you only accept Keycodes that represent 0-9, you don’t have the ability to use your backspace key, or your left and right arrow keys, or the return key, and so forth. So to get around this, in a field you’re limited to numbers only, you have to include these function keys as well as Keycode 190, which represents the period or decimal point, assuming you want to allow decimals in your numeric only.

We could, of course, list all 52 Alpha character Keycodes, and set them to zero if they were pressed, but that’s rather cumbersome, so we’ll use the 48-57 plus 190 plus the function keys, and if they’re pressed we’ll leave the Keycode equal to the Keycode. If anything else is pressed, we’ll assign the Keycode, once again, to zero.

Code:
Private Sub NumericsOnlyAllowed_KeyDown(KeyCode As Integer, Shift As Integer)
 Select Case KeyCode
    Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 190, vbKeySpace, vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft
       KeyCode = KeyCode
    Case Else
       KeyCode = 0
    End Select
End Sub

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Many thanks, very useful. Thanks for taking the time on it. I gave a star earlier, owe you one. Regards
 
Glad to help! If you're in the States, Happy Thanksgiving!

Linq

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Thanks, I'm in the UK. Best regards
 
I was wanting this very thing for my txtbox. My problem is how do I keep the change event from happening. I can type in 123 then "a" and the cursor returns to the begining of the string in the textbox, then typing in any other char. clears the box alltogether. If I don't add some sort of code(below) in the KeyUp or KeyDown event then nothing works. How do I send a Keycode(35) to return the cursor to the end of the string?
If Len(txtLeakKey.Text) > 0 Then txtLeakKey.Text =
Left(txtLeakKey.Text, Len(txtLeakKey.Text) - 1)

That darn cat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top