Connecting Tech Pros Worldwide Help | Site Map

Use IsCharAlphaNumeric() Rather Than ASCII Values

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#1   Jun 17 '07
Many times we need to determine if a particular character, whether alone, or among other characters in a String, is an AlphaNumeric character (if it falls in the range of characters from A - Z, or a to z, or 0 - 9). The traditional method of doing this in VBA is to compare the Asc(UCase(character)) to the ANSI values for the ranges. The Windows API, however, provides a Function designed specifically for this purpose IsCharAlphaNumeric(). IsCharAlphaNumeric() is significantly faster than its counterpart. You simply pass the ASCII value of the character as an Argument to this Function and it returns True if the character is AlphaNumeric and False if it is not. Both the slow and faster versions of the code will be illustrated below. Be sure to Declare the API Function(s) in the Declarations Section of a Module.
  1. Slow version of the code:

    Expand|Select|Wrap|Line Numbers
    1.  strTestChar As String, intTestChar As Integer
    2. Const conFirstChar = 65
    3. Const conLastChar = 90
    4. Const conFirstDigit = 48
    5. Const conLastDigit = 57
    6.  
    7. strTestChar = "4"
    8. intTestChar = Asc(UCase(strTestChar))
    9.  
    10. If (intTestChar >= conFirstChar And intTestChar <= conLastChar) Or _
    11. (intTestChar >= conFirstDigit And intTestChar <= conLastDigit) Then
    12. 'the character is AlphaNumeric
    13. Else
    14. 'the character is Not AlphaNumeric
    15. End If
  2. Faster and more efficient version of the code:

    Expand|Select|Wrap|Line Numbers
    1.  Private Declare Function IsCharAlphaNumeric Lib "User32" Alias "IsCharAlphaNumericA" (ByVal cChar As Byte) As Long
    2. Private Declare Function IsCharAlpha Lib "User32" Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long
    Expand|Select|Wrap|Line Numbers
    1. IsCharAlphaNumeric(Asc(strTestChar)) Then
    2. 'the character is AlphaNumeric
    3. Else
    4. 'the character is Not AlphaNumeric
    5. End If
    6.  
    7. 'Similiar Function to test for Alphas
    8. If IsCharAlpha(Asc(strTestChar)) Then
    9. 'the character is an Alpha
    10. Else
    11. 'the character is Not an Alpha
    12. End If
  3. NOTE: There is also a similar API Function (IsCharAlpha()) that checks a character to see whether itis an Alpha (a - z or A - Z). Its code and Declaration is also listed.



Newbie
 
Join Date: Dec 2007
Location: Massachusetts
Posts: 1
#2   Dec 11 '07

re: Use IsCharAlphaNumeric() Rather Than ASCII Values


I'm trying this out. The business need is having to filter out records containing fields where non alphanumeric (@, ., !) characters are contained within one of its fields. The remaining records are those which have to be researched. Eliminating the non-pertinent records using automation will save approx. 20 minutes each time this audit is performed. Thanks!

I'll try to report back to the thread on whether or not this application of the method was successfull.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#3   Dec 11 '07

re: Use IsCharAlphaNumeric() Rather Than ASCII Values


Quote:

Originally Posted by Chris Austin

I'm trying this out. The business need is having to filter out records containing fields where non alphanumeric (@, ., !) characters are contained within one of its fields. The remaining records are those which have to be researched. Eliminating the non-pertinent records using automation will save approx. 20 minutes each time this audit is performed. Thanks!

I'll try to report back to the thread on whether or not this application of the method was successfull.

Hope this works out for you, it is the most efficient Method for establishing whether or not a character is AlphaNumeric.
Reply