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



The information in this article applies to:

  • Microsoft Access 1.0
  • Microsoft Access 1.1
  • Microsoft Access 2.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97

This article was previously published under Q130333
Moderate: Requires basic macro, coding, and interoperability skills.

SUMMARY

This article shows you how to create a sample user-defined function 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.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming kbusage KB130333