ACC: How to Sort Records in Case-Sensitive ( ASCII) Order

Last reviewed: August 29, 1997
Article ID: Q130333
The information in this article applies to:
  • Microsoft Access version 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article demonstrates a sample user-defined function you can use to sort records in case-sensitive (or ASCII) order.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.

MORE INFORMATION

Microsoft Access sorts records in ascending or descending order without regard to case. However, you can use a user-defined function in a query to sort text data by its ASCII character values. This results in a case- sensitive order.

The following table demonstrates how the ascending order in Microsoft Access differs from a case-sensitive order:

   Ascending   Case-Sensitive
    Order          Order
   --------------------------
        a              A
        A              B
        B              C
        b              D
        c              a
        C              b
        D              c
        d              d

To sort records in case-sensitive order, follow these steps:

  1. Open an existing Microsoft Access database.

  2. Create a new table with the following structure:

          Table: Sorting Test
          -------------------
          Field Name: Test
    
             Data Type: Text
    
    

  3. View the Sorting Test table in Datasheet view, type the following eight records in the Test field, and then close the table:

          b
          d
          B
          A
          D
          a
          C
          c
    

  4. Create a module and type the following line in the Declarations section if it is not already there:

          Option Explicit
    

  5. Type the following procedure:

          Function StrToHex (S As Variant) As Variant
          '
          ' Converts a string to a series of hexadecimal digits.
          ' For example, StrToHex(Chr(9) & "A~") returns 09417E.
          '
    
             Dim Temp As String, I As Integer
                If VarType(S) <> 8 Then
                   StrToHex = S
                Else
                   Temp = ""
                For I = 1 To Len(S)
                   Temp = Temp & Format(Hex(Asc(Mid(S, I, 1))), "00")
                Next I
                StrToHex = Temp
             End If
          End Function
    
    

  6. Create the following query based on the Sorting Test table:

          Query: CaseSensitive Sorting Test
          ---------------------------------
          Type: Select
          Field: Test
    
             Sort: not sorted
             Show: True
          Field: Expr1: StrToHex([Test])
             Sort: Ascending
             Show: False
    
    

  7. Run the query. Note that the records are sorted in case-sensitive order. The uppercase characters (A-D) appear before the lowercase characters (a-d).

REFERENCES

For more information about the Hex() function search the Help Index for "Hex function," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbusage PgmHowTo
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.