Keyboard Input

Sometimes you need to write your own routines to limit what a user can type. Othertimes, you might need to check for a specific key (such as Esc or F1).

Visual Basic 6.0

(These note are based on MS Access 97 which uses VBA)

I had a requirement for an unbound data field which accepts only numbers - "+", "-", and letters are not allowed. The obvious solution was to try an InputMask. However, both of these

where unacceptable - yes, they limited the input to numbers but they produced a limited, un-natural space to type the data. In addition, the first one produced confusing error messages.

My solution was to process each key as it is pressed making the interface natural and intuitive. (Well at least I think so.)

For controls with a KeyPress event (Text Box and Combo Box), all keystrokes which generate an ASCII code are sent to the event if the On Key Press property is set. Since the Arrow keys and the Del key do not have ASCII codes, they are processed elsewhere and not sent to this event. Notice that the Backspace (vbBack) key does have an ASCII representation (8) and will be sent to the event.

This code simply removes all characters except numbers (0 to 9) and the Backspace.

If you want to process the non-ASCII keys - Function keys, Arrow keys, the Del key, and the like - use the KeyPress event.

Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Languages / Keyboard_Input.htm