Solved

sorting records in access 2000 query within vb6.0

Posted on 2008-11-01
7
432 Views
Last Modified: 2013-12-25
I developed a query in access2000 using an existing table, displayed the query using sql view, then copied the query into a vb6.0 program.  The query sorts a column in ascending order.  The column is a text field. The information in the column of the records varies in length from 1 to 2 positions.

For example, the data in the column of some of the records  is 1.  In other records the data might be 11, or 12 or 2 or 3, etc..  

I would like the the sort to arrange the records with the 2 ahead of the records with 11, however, the query sorts as follows:

1
1
11
12
2
3
etc.

I would like for it to be
1
2
3
11
12
etc.

How can I accomplish this?

 
0
Comment
Question by:morrisbo
7 Comments
 
LVL 18

Expert Comment

by:jmoss111
ID: 22860345
Hello morrisbo,

You could convert the column using CLng([MyColumnName]) then it would sort correctly

Regards,

Jim
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22860453
I agree with Jim.  This is how I would do this as well.  It works in most SQL environments to just convert in order by statement.

SELECT Col1, Col2, Col3
FROM TableName
ORDER BY CLng(Col1);

OR Val(Col1)

Again, this is just an extension of what Jim already told you -- his answer is correct.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22861267
If the column you are sorting on only ever contains numbers, why is the column type declared as Text in the database.  If it were declared as a number type, then the sort would work properly, without the need to convert the column data in the SQL.

AW
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:morrisbo
ID: 22862651
Thanks for the input.

I mispoke about the data.  It was declared as text as some of the records contain values other than numbers.

It actually contains Insurance Territories in Texas

example:

1
2
3
4
5
6
11
15
15C
etc.

I think I can work with this though.  Just need to convert the values with Alpha characters to some unizue number that will cause it to sort correctly, then convert it back when printing the results.

Thanks,

I will give it a try.

morrisbo
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22862681
The VAL function will return the numbers portion of a column, so if the value is always numbers followed by a letter you can do this in the order by.
+Orders by number portion as a number
+Orders by field as text within each number
SELECT Col1, Col2, Col3
FROM TableName
ORDER BY VAL(Col1), Col1;
 
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22862707
If some of the values are numbers and the others that are text can be any pattern, then do this to sort numbers and text independently:
SELECT Col1, Col2, Col3
FROM TableName
ORDER BY IIF(IsNumeric (Col1), VAL(Col1), 0), Col1;
If you want text to appear after numbers, then you substitute the 0 for big number (i.e. bigger than max value of number based codes, so if most 2 digits -- 999 would be big in comparison).  Again, add second sort on the column itself to handle alpha sort.
0
 

Author Comment

by:morrisbo
ID: 22871173
I created a column expression val([Territory}) and made this the major sort ascending, then sorted on the existing Territory next.  This gave the result I needed.

Thanks for all the help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now