Visual Basic 6.0
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
099999;;" " 999999;;" "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.
Private Sub SomeNumber_UIEdit_KeyPress(KeyAscii As Integer) ' Do not allow anything but numbers If (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) And _ KeyAscii <> Asc(vbBack) Then KeyAscii = 0 End If End SubIf you want to process the non-ASCII keys - Function keys, Arrow keys, the Del key, and the like - use the KeyPress event.