Classify Keypresses in a Language-Independent Manner

Problem

You need to be able to classify a keypress as a character, a digit, or neither. You also need to know if a character is uppercase or lowercase. You know you can write code to handle this, but if you do that, you’re limiting yourself to a single national language, since languages classify their characters differently. Since Windows knows about various character sets, is there some way you can use Windows to do this work for you?

Solution

You could write VBA code to classify characters, but it wouldn’t be language-independent. For example, the ANSI character 65 is an uppercase character in the standard multinational character set, but it may be different in another character set. If you want your applications to work in various languages, you must not assume specific character ranges. The Windows API includes a number of functions you can call to categorize characters based on their ANSI values. The isCharAlpha and isCharAlphaNumeric functions both are faster than the built-in VBA functions and are able to deal with international issues. Luckily, an ANSI value is exactly what the KeyPress event procedure in Access sends you, so you can use these functions from within KeyPress event procedures that you write.

In addition to the necessary function declarations, the sample database 11-03.MDB includes a demonstration form showing all the ANSI characters and their classifications. Load and run frmCharClasses from 11-03.MDB ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.