Microsoft KB Archive/130333

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

Article ID: 130333

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



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:

 Open an existing Microsoft Access database.  Create a new table with the following structure:      Table: Sorting Test ---     Field Name: Test Data Type: Text  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

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

Option Explicit

  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 </li>  Create the following query based on the Sorting Test table: <pre class="fixed_text">     Query: CaseSensitive Sorting Test -     Type: Select Field: Test Sort: not sorted Show: True Field: Expr1: StrToHex([Test]) Sort: Ascending Show: False </li> 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).</li></ol>

<div class="references_section">