Solved

Access: Sorting Months in a query in the correct order

Posted on 2007-11-27
13
1,674 Views
Last Modified: 2009-01-22
I have a query that sorts out months in the following format:

Field:              Month                
Row1         August 2006                        
Row2         September 2006            
Row3         October 2006                  
Row4         November 2006              
Row5         December 2006
Row6         January 2007
Row7         February 2007
Row8         March 2007
Row9         April 2007
Row10         May 2007
Row11         June 2007
Row12         July 2007
Row13         August 2007

The problem is that sometimes (Unlike above) the months are in the wrong order. How do I sort the month column to insure that the month and year are always in the correct order (As above)            
0
Comment
Question by:ouestque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20361460
what is the actual input field? is it datetime, or is it already a string in that format.
0
 
LVL 8

Accepted Solution

by:
Emil_Gray earned 500 total points
ID: 20361505
If you are using a query then it is easy. Create a new field just for sorting purposes. Format your original date field there as follows:

Format([mydate], "yyyymmdd")
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20361532
select * from NameofTable
order by format([Month],"yyyymm)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:jerryb30
ID: 20361590
If text, Order By datevalue(MonthAndYear)
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 20361638
As I said initially since I presume you are using a query create a new field in the query. I'll call it myDate for the purpose of explaining.

Format the field as a Date/Time field. In the query the field would look like myDate: Format([MonthYear], "yyyymmdd") where [MonthYear] is the name of your field in the query that holds the data you are trying to sort. Then you sort the myDate field as you wish.
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 20361696
If it is a date field, it will order regardless of how you format.  
If it is a text field, you will not get format to handle it anyway.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20361758
<If it is a text field, you will not get format to handle it anyway.>

why not try my post.

  select * from NameofTable
order by format([Month],"yyyymm)
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 20361778
?format("january 2007","yyyymm")  will give you

200701
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 20361795
I am amazed. I bow to your superior intellect. Yet again.
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 20363599
capricorn1 and jerryb30, if the field showing the Month and Year that the questioner provided is a Date/Time field then the solution I first proposed is the easiest answer. This is not rocket science.

Format a new field as a Date/Time field. In your query the field would look like;

myDate: Format([MonthYear], "yyyymmdd")

where [MonthYear] is the name of your field in the query that holds the data you are trying to sort. Then you sort the myDate field as you wish either Ascending or Descending.
0
 

Author Comment

by:ouestque
ID: 20460500
Thank ya'll so much!!! I really appreciate you help. Emil_Gray posted the correct answer first and therefore she gets the points. Thanks!!!
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 20460914
ouestque, thank you. However I am a male not a female.

Emil Gray
0
 

Expert Comment

by:cspbarnes
ID: 23442999
And if you looking to only do it for the month, let's say a Birthday list, then you would use
myDate: Format([Month], "mm")
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

728 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