Link to home
Start Free TrialLog in
Avatar of Kylee Davis
Kylee Davis

asked on

Sorting Text as a number in Access

Is there a way to sort text as numbers in access without omitting the data that contains text values? I am currently trying to sort by operation which includes the values 1,2,2A,3,10,11,12,....ect. When I currently sort these values because the are stored as text they appear as 1,10,11,12,2,2A,3.......ect. I have tried using the expression Expr1: IIf([Operation] Is Null, 0, Val([Operation])) but it omits the operations that contain text values such as 2A. Is there a way to sort this information correctly?
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

<<but it omits the operations that contain text values such as 2A. >>

 It should not.  Val("2A") will return 2.

Jim.
If all values start with the numeric part, then as Jim said, Val() will solve your problem.  If the value has embedded letters or special characters, then that requires a code solution.  I created a custom solution for a construction company because their drawing numbers were built in three parts and each part might have letters and numbers.  Essentially, it involves pulling out all the numeric parts and fixing them in width by right justifying with leading zeros.  So 2 becomes 0002 and 10 becomes 0010 and that makes 2 sort before 10.  I didn't post the code I wrote because it wouldn't help you unless your codes followed the same rules as those for the Drawing Log app I built.
Avatar of Kylee Davis
Kylee Davis

ASKER

Pat that is the issue that I am currently having. The text values need to be included because they help to distinguish between different machines. For example operation 2 might be Saw-1 and operation 2A might be Lathe-3. I am trying to figure out how to write a code that will pad my numbers so that they will sort as you stated above. Any suggestions on how to go about doing that?
As long as the number is in the front, then val() will work fine.

  But you can't have "2A3" and get 23 with val().  It will always return 2.

Jim.
I didn't say to ignore the text values.  I said you have to standardize the numeric values so they are all the same length.  If the text parts can be variable length, they will also cause sorting issues so they will also need to be right justified.  You have to create a field on which you sort.  It doesn't ever have to be displayed.  It just needs to be used for sorting.  Your code needs to construct something like:

0002???A
0010??AB

I used ? as the filler for the text because you can't "see" spaces and it really doesn't matter what character you use.  The point is you have to RIGHT-JUSTIFY text strings to sort them.

So although you can use Val() to extract the leading numbers, that doesn't really help with the letters.  My code looped through the fields character by character to build the sort string.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.