Sorting Text Fields Containing Letters and Numbers

To whom it may confuse. I have a query with a text field that needs to be 
sorted in descending order. The field name is "ContainerNumber" and the data 
will always have at least a letter in the begining. As you can see the data 
sample "D100/07" comes after "D10/07" and that is not what I need. I need 
"D11/07" to come after "D10/07" and so on. How can I fix this?

ContainerNumber
D03/07
D06/07
D07/07
D08/07
D09/07
D10/07
D100/07
D101/07
D102/07
D103/07
D104/07
D105/07
D106/07
D107/07
D108/07
D109/07
D11/07
D110/07
D111/07
D112/07
D113/07
D114/07
D115/07
D116/07
D117/07
D118/07
D119/07
D12/07
D120/07
D121/07
D13/07
D14/07
D15/07
D16/07
D17/07
D18/07
D19/07
D20/07
D21/07
D22/07
D23/07
D24/07
-- 
I feel the need....the need for code!
0
Utf
3/19/2008 10:56:11 PM
access.queries 6343 articles. 1 followers. Follow

5 Replies
1874 Views

Similar Articles

[PageSpeed] 47

Here's a function that I use for such purposes, and it may be useful "as is" 
for your needs; use it for a calculated field in the query and sort on it.

SELECT *, ReturnSortValueForAlphaNumerics([ContainerNumber]) As SortField
FROM YourTableName
ORDER BY ReturnSortValueForAlphaNumerics([ContainerNumber]);




Public Function ReturnSortValueForAlphaNumerics(ByVal strOriginal) As String
' ** LOGIC IS TO REPLACE EACH CHARACTER IN THE ORIGINAL STRING WITH A MULTI-
' ** CHARACTER "NUMBER" STRING THAT WILL SORT THE ORIGINAL STRING CORRECTLY.
Dim lngLoc As Long
Dim strSort As String, strT As String, strLoc As String

Const strDash As String = "-"
Const strNum As String = "[0-9]"


lngLoc = 1
strT = Left(strOriginal, 1)
strSort = Format(Abs(Not strT Like strNum) & IIf(IsNumeric(strT), "00", 
Asc(strT)), "000")
strT = ""

Do
    strLoc = Mid(strOriginal, lngLoc, 1)
    If strLoc Like strNum Then
        Do
            strT = strT & strLoc
            lngLoc = lngLoc + 1
            strLoc = Mid(strOriginal, lngLoc, 1)
        Loop While strLoc Like strNum
        strSort = strSort & Right("!!!!!!!!!!" & CStr(Val(strT)), 10)
        strT = ""

    Else
        If strLoc = strDash Then
            strSort = strSort & "AAA"
        Else
            strSort = strSort & strLoc & "ZZ"
        End If
        lngLoc = lngLoc + 1
    End If

Loop Until lngLoc > Len(strOriginal)

ReturnSortValueForAlphaNumerics = strSort

End Function

-- 

        Ken Snell
<MS ACCESS MVP>



"code_hungry" <codehungry@discussions.microsoft.com> wrote in message 
news:B4D6723E-080D-4EC9-BB6F-05A45BF16CE1@microsoft.com...
> To whom it may confuse. I have a query with a text field that needs to be
> sorted in descending order. The field name is "ContainerNumber" and the 
> data
> will always have at least a letter in the begining. As you can see the 
> data
> sample "D100/07" comes after "D10/07" and that is not what I need. I need
> "D11/07" to come after "D10/07" and so on. How can I fix this?
>
> ContainerNumber
> D03/07
> D06/07
> D07/07
> D08/07
> D09/07
> D10/07
> D100/07
> D101/07
> D102/07
> D103/07
> D104/07
> D105/07
> D106/07
> D107/07
> D108/07
> D109/07
> D11/07
> D110/07
> D111/07
> D112/07
> D113/07
> D114/07
> D115/07
> D116/07
> D117/07
> D118/07
> D119/07
> D12/07
> D120/07
> D121/07
> D13/07
> D14/07
> D15/07
> D16/07
> D17/07
> D18/07
> D19/07
> D20/07
> D21/07
> D22/07
> D23/07
> D24/07
> -- 
> I feel the need....the need for code! 


0
Ken
3/19/2008 11:35:58 PM
code_hungry wrote:

>To whom it may confuse. I have a query with a text field that needs to be 
>sorted in descending order. The field name is "ContainerNumber" and the data 
>will always have at least a letter in the begining. As you can see the data 
>sample "D100/07" comes after "D10/07" and that is not what I need. I need 
>"D11/07" to come after "D10/07" and so on. How can I fix this?


You can use a public function in a standard module to
calculate an sortable string.  Here's some air code with the
general idea:

Public Function ContNum(CN As String) As String
Dim pos As Integer

For pos = 1 To Len(CN)
	If Mid(CN, pos, 1) Like "[0-9]" Then Exit For
Next pos
ContNum = Left(Left(CN, pos - 1) & String(" ", 7), 7)

ContNum = ContNum & Format(Val(Mif(

-- 
Marsh
MVP [MS Access]
0
Marshall
3/20/2008 1:09:14 AM
Oops,

to continue:

Public Function ContNum(CN As String) As String
Dim pos As Integer

For pos = 1 To Len(CN)
	If Mid(CN, pos, 1) Like "[0-9]" Then Exit For
Next pos
ContNum = Left(Left(CN, pos - 1) & Space(7), 7)

ContNum = ContNum & Format(Val(Mid(CN, pos)), String("0",8))

Pos = InStr(CN, "/") + 1
ContNum = ContNum & Format(Val(Mid(CN, pos), String("0",6))

End Function

Then sort on the calculated field:
	ContNum(ContainerNumber)

-- 
Marsh
MVP [MS Access]
0
Marshall
3/20/2008 1:22:03 AM
Marshall:

Re last line:  You haven't closed the Val() function.  Believe it should read:


ContNum = ContNum & Format(val(Mid(CN, pos)), String("0", 6))

Bob

Marshall Barton wrote:
>Oops,
>
>to continue:
>
>Public Function ContNum(CN As String) As String
>Dim pos As Integer
>
>For pos = 1 To Len(CN)
>	If Mid(CN, pos, 1) Like "[0-9]" Then Exit For
>Next pos
>ContNum = Left(Left(CN, pos - 1) & Space(7), 7)
>
>ContNum = ContNum & Format(Val(Mid(CN, pos)), String("0",8))
>
>Pos = InStr(CN, "/") + 1
>ContNum = ContNum & Format(Val(Mid(CN, pos), String("0",6))
>
>End Function
>
>Then sort on the calculated field:
>	ContNum(ContainerNumber)
>

-- 
Message posted via http://www.accessmonster.com

0
raskew
3/20/2008 1:36:21 AM
raskew via AccessMonster.com wrote:

>Marshall:
>
>Re last line:  You haven't closed the Val() function.  Believe it should read:
>
>
>ContNum = ContNum & Format(val(Mid(CN, pos)), String("0", 6))


Right!

-- 
Marsh
MVP [MS Access]
0
Marshall
3/20/2008 4:02:16 PM
Reply:

Similar Artilces:

Linking Outlook fields from the "Contact Folder" to a custom form in Outlook...
Using Exchange Server 2003/Outlook 2003, we have created a custom Help Desk form in Outlook. We would like the ability to link specific fields from the Contacts Folder (Example B), in a drop-down list box if possible. We are far from knowing that something like this is even possible. Would appreciate *any* suggestions. Thank you. ...

Replacing the Numbers
In my report I want to replace the value of my text data to a word. Example: in my report the TextBox: is (loc) which the "1", "2", "3" these are the records in the tables. They print out on the reports as 1, 2, and 3. I want to change that to read 1=ED, 2=EU and 3=EZ. And blank, if the user leave it blank as "NONE" I hope that's clear Thanks Check out your more recent thread with the same question. -- Duane Hookom MS Access MVP "bladelock" <bladelock@discussions.microsoft.com> wrote in message news:1FB81F68-1733-4174-8D...

Where's the Sort by New Message option?
Not sure when the layout of the newsgroups changed but I can't seem to find the ability to sort by new message in a thread instead of sorting by just the original thread date. That was hugely helpful as I could follow along and see what threads were still active. If you're gonna hang around in the newsgroups (any newsgroups), you may want to start using a newsreader. Microsoft Outlook Express can do it for you. Saved from a previous post: You may want to connect to the ms newsserver directly: If you have Outlook Express installed, try clicking on these links (or copy and paste i...

Outlook 2002 Contact Phone Number Field Formatting
Why don't phone numbers entered in the phone number contact fields on Outlook 2002 automatically format to include the paren's and hyphens? If this is suppose to happen, what is the setting to make it happen? They do here and always have. What are you seeing? --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. After searching google.groups.com and finding no answer mike <michael.kelner@fmr.com> asked: | Why don't phone numbers entered in the phone number | contact fields on Outlook 2002 automatically format to | include th...

Sorting
Hi, is it possible to use variable for sorting? Something like: declare @sort varchar(10) SET @sort='column1 DESC' SELECT ROW_NUMBER() OVER (ORDER BY @sort)AS rowId, * FROM table Is dynamic SQL only option? Thank you, Simon On 15/04/2010 11:28, simon wrote: > Hi, > > is it possible to use variable for sorting? > > Something like: > > declare @sort varchar(10) > SET @sort='column1 DESC' > > SELECT ROW_NUMBER() OVER (ORDER BY @sort)AS rowId, * FROM table > > Is dynamic SQL only option? You can use CASE to he...

How to sort account list?
I'm using MS Money 2004 Small Business. In the account list, my accounts are catagorized by and in this order: - bank accounts - credit accounts - investment accounts - loans and liabilities - asset accounts TOTAL BALANCE In the above view, I cannot easily determine totals for assets and liabilities. The following view does this. - bank accounts - investment accounts - asset accounts SUBTOTAL -loans and liabilities SUBTOTAL TOTAL BALANCE How do I get the above view? Thanks, Brett I don't use Small Business. But you can run a Net Worth report to get total assets and ...

Row names will not move with sort
Each row in my summary table in Sheet1 is hyperlinked to a corresponding table in Sheet2 with additional details. I need to move the rows in my summary table (Sheet1) and preserve the links to and from the details tables (Sheet2). I found that I can move rows in Sheet1 with cut & insert and the hyperlinks are maintained. I can travel from a row in summary table (Sheet1) to details table (Sheet2) and back, even after the corresponding row (Sheet1) was moved. But if I sort the rows in Sheet1 the links from Sheet2 back to corresponding rows in Sheet1 lead to presorted locations. Apparen...

show continous fields
I would like a control (or subform) on a form to behave similar to a report field set to "can grow" and "can shrink". I want to view several records continously, not broken up like the continuous forms requires, but more like a report that shows the data continuously. I tried putting report on a form like a subform, but MS Access doesn't go for that. Is there any way to accomplish this? In this setting the data is for viewing only. -- sam Yo can use the form record source to remove the records that you don't want to display, if Null or Empty? Why do you need...

Text to Number conversion
Hi all, Need some help. One of my main data for my job, is in Excel format. The problem lies with the cell format. All number being stored as text. I need to do error checking and then keep pressing C on the keyboard ( for converting to number ) . It took between 30 to 45 minutes pressing time before it finally completed. Is there any way to remove this "Pressing Job" as it really irritates me. I don't blame you ...... Following from Excel Help:- 1.. Select a blank cell that you know has the General number format.=20 2.. In the cell, type 1, and then press ENTER.=20 ...

Secondary lookup field in CRM 4.0
Hello all. I have a question regarding secondary lookup field within the form of the Opportunity entity on CRM 4.0 (upgraded from 3.0). To be able to display a secondary lookup (pointed to contacts), I have used a JScript (as well as two new attributes) on the form's onLoad event (pasted below). It worked fine on CRM 3.0 but after the upgrade to 4.0, the form doesn't report any JScript error but no lookup field is displayed - only the lookup icon is there and it doesn't respond to clicking. I haven't been able to find what parts of the JScript should be changed in order...

sorts not sorting
OK- Thanks to everyones help I now i have a list i can work with... but there are a few problems.... the zips are all 5 digit and now i have to sort them... the problem is, THEY WONT SORT CORRECTLY!!!!!!! The numbers are not in order... some are but a lot are not... is there anyway to fix this???? i need to sort them by zip code but cant seem to get them to do it correctly.. any ideas??? ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~ View and post usenet messages directly from http://www.ExcelForum.com/ You are going to be far better of...

import- number- sort?
I imported this data into a spread sheet- I tried to format it to number, but when I sort it- I get : 37.08 4.33 4.79 4.8 42.56 5.94 Whats up with this? Thanks Brent Hi Brent, Install the TrimAll macro, select the column and invoke the macro from Alt+F8 http://www.mvps.org/dmcritchie/excel/join.htm#trimall Directions to install and use a macro at http://www.mvps.org/dmcritchie/excel/getstarted.htm#havemacro I expect that you have spaces or CHAR(128) "Required Blank" characters in you data you can check is =LEN(A1) --- HTH, David...

sorting macro #3
I need to sort on 8 cells in a row and on 1500+rows. I can do it one at a time but I know there must be an easier way. Can ANYONE HELP? C3:J3 I need these cells sorted and then down to C1532:J1532 I did create a macro to sort after I selected them but still one at a time. Does it have to be a macro? I can edit a macro but don't really know how to program them. Yes, it has to be a macro. The following should do it. This macro loops through all the entries in Column C, and in each row it sorts the values in Columns C:J. HTH Otto Sub SortRows() Dim rColC As Range Dim i As...

amounts with letter inside the invoices
Since they can add amounts with letter inside the invoices, since a field does not exist as(like) in Checks that there exists a STRG A255 that turns the last occurrence of the information into letters, That it is possible to do, does not exist an opcion like that for the part of sales, or a SP has to be realized thanks, J Villar's ...

text in cell shows up as ####
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Text in cells in a coumn shows up as ####, apparently because there is a large amount of text. When I mouse over, the text shows. It also shows up in the formula bar. How can I copy and or print the text in these cell? <br><br>When I try to save these cells as text and transfer to Word, they continue to print as ###. text in cell shows up as #### <br> I am totally new to this whole environment! A new iMAC and this software. <br> However, in trying to learn how to use EXCEL, I found that...

overlap text boxes
I am using Publisher 2002, but an officemate only has 2000, so that is the format in which I am saving. I would like to be able to "overlap" two text boxes, so that the positioning of one slightly overlapping the other does not make the text in either move. I keep looking at the options under "Format Text Box," and none of them seems to do this. Do you have any suggestions? Select the text boxes, set the wrap to none... -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "sorrywm" <sorrywm@discussions.m...

how2: show contact name and phone fields in task list
It would be useful if I could display contact name and phone number fields in my task list. In Simple View I've used Field Chooser to insert these columns into the table .. but alas! the fields are blank. Can I do what I want to do and .. how? thanks Any folder view shows only the data in that folder, even though Outlook lets you think otherwise by allowing you to add fields from different types of items in the folder. You may be able to accomplish what you want, however, with a little custom VBA code. For an example, see http://www.outlookcode.com/codedetail.aspx?id=566 -- Su...

Can we sort a form and also be able to enter new records.
Hi, It is possible to sort the data on a form and also be able to enter new and edit the existing records in it. Cheers! On Tue, 15 Jan 2008 15:41:16 -0800 (PST), Rex wrote: > Hi, > > It is possible to sort the data on a form and also be able to enter > new and edit the existing records in it. > > Cheers! I assume you mean "Is it". The form sort order has nothing to do with editing or adding new records, so the answer is yes. If you cannot edit or add new records, make sure the form's record source, if it is a query, is updateable. Some queries are not. O...

Sorting checking register
How do i sort my check register in Money 2004 Deluxe so that uncleared transactions appear below cleared transactions? In Quicken i could click on the column to provide that sort. In microsoft.public.money, Dean wrote: >How do i sort my check register in Money 2004 Deluxe so >that uncleared transactions appear below cleared >transactions? In Quicken i could click on the column to >provide that sort. You don't. Available choices are chosen by clicking View at the top of the register. ...

Logical Test: Text Against Number 01-05-10
Can anyone provide insight into how Excel treats this type of test? For example, why does Excel return TRUE when we have ="a">2 but FALSE when ="a"<2 (or any other number for that matter). Thanks. That's the way it is treated. Any kind of text is larger than the largest number. Any worth? hit the YES below -- Max Singapore --- "rslaughter5" wrote: > Can anyone provide insight into how Excel treats this type of test? For > example, why does Excel return TRUE when we have ="a">2 but FALSE when ="a"<2 ...

MS Access excel-OLE Container Control Resize
In MS ACCESS 2003, is there anyway to programmatically resize the in-Process excel OLE container control after activation. Regards Sivanand FITSI, India. ...

Text in Cell as Formula #2
Peo + Julie Many thanks for your answer with the last query. I Have a similar question, please advise: This method [INDIRECT()] does work with values form a different spreadsheet. I type the link (as text) from a different spreadsheet in a cell in the current spreadsheet and use this method to call a value in a cell. Would appreciate a detailed answer (if complicated) - I'm a novice user! Many thanks in advance. ...

Rotate text?
Office(publisher) 2K. Is there anyway to rotate a specific word in a sentence? L. Not if it's part of the sentence rather than being a separate text box. -- JoAnn Paules MVP Microsoft [Publisher] "L." <ofallon12320@yahoo1.com> wrote in message news:%23TBzesS9FHA.2616@TK2MSFTNGP15.phx.gbl... > Office(publisher) 2K. > > Is there anyway to rotate a specific word in a sentence? > > L. > > Thanks for the info. L. "JoAnn Paules [MSFT MVP]" <jl_paules@hotmail.com> wrote in message news:U46dnd7PfZ5CTBHenZ2dnUVZ_sidnZ2d@suscom....

Increment Number Without Using Autonumber
Hi Folks - I have a db that tracks felony charges against defendants. I have a main defendant form with a charges subform. On the subform, I have 2 fields: Count and Statute. Count is a number from 1 to 999. Statute is a drop-down that references the master statute table. Currently, the user will enter the count manually. However, I would like to automate this process if possible. One problem with the manual approach is if Count 3 of 10 Counts is deleted. This forces the user to re-number the Counts. Not sure how to handle this. Any ideas? Note: For each charge, there can be multiple v...

Plain text warning/ VS2005
Whenever I add a CRecordset derived class, I get an error telling me the password for the DB is sent in "plain text" and is not secure etc. I comment that line out, but how much of a security risk is this? What can I do with my Access database to avoid this? VC 6.0 never gave this error... tia b If you are on any kind of network that anyone can look at, you are wide open for attack. Example, a University network; potentially, ever student can be running a packet sniffer and see your password. Anything that passes acros the Internet outside your local intranet is also vuln...