473,499 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting text in numeric order

I have a field with values like this

CO 03-10
CO 03-4
VI 03-8
CO 03-533

I would like these to sort for a report by the first two letters and the
digits after the hyphen (-) like this

CO 03-4
CO 03-10
CO 03-533
VI 03-8

I can get them to sort by the first two letters or the last few digits
but not both. Any ideas?

Thank you,
Deborah


Nov 12 '05 #1
4 8871
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To sort "text-numbers" as numerics you have to have leading zeros.
E.g.:

CO 03-010
CO 03-004
VI 03-008
CO 03-533

This will sort to:

CO 03-004
CO 03-010
CO 03-533
VI 03-008

You can put leading zeros on text numbers like this:

If strNumber = "2" then Format(strNumber, "000") will produce "002".

HTH,

MGFoster:::mgf
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP5hhGIechKqOuFEgEQKqmQCfY9Uft5LueIPOhFzj4AVbUR lCrkgAn0j9
xrYKe+6yzXpzFROW3tXqFIwk
=ii4g
-----END PGP SIGNATURE-----

Deborah V. Gardner wrote:
I have a field with values like this

CO 03-10
CO 03-4
VI 03-8
CO 03-533

I would like these to sort for a report by the first two letters and the
digits after the hyphen (-) like this

CO 03-4
CO 03-10
CO 03-533
VI 03-8

I can get them to sort by the first two letters or the last few digits
but not both. Any ideas?

Thank you,
Deborah


Nov 12 '05 #2

"Deborah V. Gardner" <dg******@twcny.rr.com> wrote in message
news:3F***************@twcny.rr.com...
I have a field with values like this

CO 03-10
CO 03-4
VI 03-8
CO 03-533

I would like these to sort for a report by the first two letters and the
digits after the hyphen (-) like this

CO 03-4
CO 03-10
CO 03-533
VI 03-8

I can get them to sort by the first two letters or the last few digits
but not both. Any ideas?

Thank you,
Deborah


First you will have to use a query. In the query you will need to reference
a function that has to be in some module. The function should look
something like

Function SortFunction(InputString as String) as String
Dim DashPosition as Integer
Dim FirstPart as String
Dim LastPart as String
LastPartFormatted as String
DashPosition = Instr(InputString,"-")
FirstPart = Left(InputString, 2)
LastPart = Mid(InputString, DashPosition + 1)
LastPartFormatted = Format(Val(LastPart),"000")
SortFunction = FirstPart & LastPart

In the query you will use SortFunction(FieldName) as a field in the query,
and then sort on it.

Hope this helps,

Gary
Nov 12 '05 #3
Thank you for the suggestions. I really wanted to avoid padding the numbers with
zeroes as you and MG Foster suggested. So far, this is what I have.

Dim intPostion As Integer, intLength As Integer, intLast As Integer
Dim intLastNo As Integer

intPosition = InStr(InputString, "-")
intLength = Len(InputString)
intLast = intLength - intPosition
xSortLast = Right(InputString, intLast)

It works very well putting the items in numberical order. So, I tried to include
it in a Union query. First I built two queries, 1 for each table and used Order
By like this
Left([ComplaintNo],2), xSortLast([ComplaintNo])
(I replaced ComplaintNo with ViolationNo in the query for tblViolations)

Each query worked perfectly and put the records in numerical order by the last
few numbers. When I put the queries together they do not sort properly. I have
tried putting the Order By clause with the first half of the query; the second
half and both parts. No luck.

Any suggestions? The only other thing I can think of to do is to write the
records to a temporary table and sort that. I am hoping there is an easy method.

Thank you all once again.

Deborah

Gary Floam wrote:
"Deborah V. Gardner" <dg******@twcny.rr.com> wrote in message
news:3F***************@twcny.rr.com...
I have a field with values like this

CO 03-10
CO 03-4
VI 03-8
CO 03-533

I would like these to sort for a report by the first two letters and the
digits after the hyphen (-) like this

CO 03-4
CO 03-10
CO 03-533
VI 03-8

I can get them to sort by the first two letters or the last few digits
but not both. Any ideas?

Thank you,
Deborah


First you will have to use a query. In the query you will need to reference
a function that has to be in some module. The function should look
something like

Function SortFunction(InputString as String) as String
Dim DashPosition as Integer
Dim FirstPart as String
Dim LastPart as String
LastPartFormatted as String
DashPosition = Instr(InputString,"-")
FirstPart = Left(InputString, 2)
LastPart = Mid(InputString, DashPosition + 1)
LastPartFormatted = Format(Val(LastPart),"000")
SortFunction = FirstPart & LastPart

In the query you will use SortFunction(FieldName) as a field in the query,
and then sort on it.

Hope this helps,

Gary


Nov 12 '05 #4
I haven't been following this thread, so I'm not sure what you have and
haven't tried. Have you tried adding a computed column to your query that
pads the numbers with zeroes and sorting on that field?

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"Deborah V. Gardner" <dg******@twcny.rr.com> wrote in message
news:3F***************@twcny.rr.com...
Thank you for the suggestions. I really wanted to avoid padding the numbers with zeroes as you and MG Foster suggested. So far, this is what I have.

Dim intPostion As Integer, intLength As Integer, intLast As Integer
Dim intLastNo As Integer

intPosition = InStr(InputString, "-")
intLength = Len(InputString)
intLast = intLength - intPosition
xSortLast = Right(InputString, intLast)

It works very well putting the items in numberical order. So, I tried to include it in a Union query. First I built two queries, 1 for each table and used Order By like this
Left([ComplaintNo],2), xSortLast([ComplaintNo])
(I replaced ComplaintNo with ViolationNo in the query for tblViolations)
Each query worked perfectly and put the records in numerical order by the last few numbers. When I put the queries together they do not sort properly. I have tried putting the Order By clause with the first half of the query; the second half and both parts. No luck.

Any suggestions? The only other thing I can think of to do is to write the
records to a temporary table and sort that. I am hoping there is an easy method.
Thank you all once again.

Deborah

Gary Floam wrote:
"Deborah V. Gardner" <dg******@twcny.rr.com> wrote in message
news:3F***************@twcny.rr.com...
I have a field with values like this

CO 03-10
CO 03-4
VI 03-8
CO 03-533

I would like these to sort for a report by the first two letters and the digits after the hyphen (-) like this

CO 03-4
CO 03-10
CO 03-533
VI 03-8

I can get them to sort by the first two letters or the last few digits
but not both. Any ideas?

Thank you,
Deborah


First you will have to use a query. In the query you will need to reference a function that has to be in some module. The function should look
something like

Function SortFunction(InputString as String) as String
Dim DashPosition as Integer
Dim FirstPart as String
Dim LastPart as String
LastPartFormatted as String
DashPosition = Instr(InputString,"-")
FirstPart = Left(InputString, 2)
LastPart = Mid(InputString, DashPosition + 1)
LastPartFormatted = Format(Val(LastPart),"000")
SortFunction = FirstPart & LastPart

In the query you will use SortFunction(FieldName) as a field in the query, and then sort on it.

Hope this helps,

Gary

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7407
by: marcelf3 | last post by:
Hello, I am a novice/moderate html programmer and I wonder how to tell the browser from which start point and to which end point in the text will each page be printed. In other words, I have a...
3
1401
by: news.hku.hk | last post by:
i have a array of objects from the class job, i.e. job teacher job teacher job teacher .............until job teacher these objects contain the string name of the teachers and ...
0
1385
by: marcelf3 | last post by:
Hello, I am a novice/moderate html programmer and I wonder how to tell the browser from which start point and to which end point in the text will each page be printed. In other words, I have a...
7
2313
by: Jason | last post by:
Is it possible to somehow sort a dataview, all on one column, but on different variables? That is, I have a 'Title' column in my dataview and I'd like to display first all "Directors" (sorted),...
5
8924
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
7
4407
by: canteyn | last post by:
Here is my problem: Structure typedef struct { char lname; char fname; int age; double salary; } employee_t;
0
1051
by: Bomac8 | last post by:
I have a text file with a four field array like this: DET-01-002737,DET-01-002737,YES,64239764b32fefc915a593f41bdb5730. My program sorts them in the order where the 3rd field says "Yes" or "NO". ...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.