Results 1 to 14 of 14
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    Sort values in a query

    Hi to all,
    In an access table a field has a Data Type as: Short text

    This fields show values as : MMM YYYY (Apr 2025, Feb 2024, Mar 2025)
    A query is built based on that table.

    Is there a way to sort these values in the query descending so that Apr 2025 is before Mar 2025 ?
    Can this be done without changing the Data Type to Date in the original table?

    For example:
    Apr 2025
    FEB 2024
    JAN 2025
    Mar 2025

    Needs to be shown as:
    Apr 2025
    Mar 2025
    JAN 2025
    FEB 2024




    Sincerely
    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Yes, use the month and year numbers and sort on them?
    Should always store dates as dates, even if you had to have the first of every month/year.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    You need the year and a numeric value for the month 1 for Jan, 2 for Feb etc

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Quote Originally Posted by Welshgasman View Post
    Yes, use the month and year numbers and sort on them?
    Should always store dates as dates, even if you had to have the first of every month/year.
    I always store dates as dates. In this case the field is linked to an Excel sheet where the format of the cells are "numbers".

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Quote Originally Posted by Khalil Handal View Post
    I always store dates as dates. In this case the field is linked to an Excel sheet where the format of the cells are "numbers".
    So just convert to a date and then sort by Year and Month.

    Edit: Are you sure they are not actually dates?, just formatted as that? That is the way I would have it. Actual Dates and formatted as mmm yyyy?
    Last edited by Welshgasman; 06-07-2025 at 08:47 AM. Reason: Removed descending then Month ascending.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Quote Originally Posted by Welshgasman View Post
    Edit: Are you sure they are not actually dates?, just formatted as that? That is the way I would have it. Actual Dates and formatted as mmm yyyy?
    IT has an input mask of: >L<LL\ 0000;0
    for the text box.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Quote Originally Posted by Khalil Handal View Post
    IT has an input mask of: >L<LL\ 0000;0
    for the text box.
    I did not know Excel had input masks?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by Khalil Handal View Post
    Hi to all,
    In an access table a field has a Data Type as: Short text

    This fields show values as : MMM YYYY (Apr 2025, Feb 2024, Mar 2025)
    Whenever I need to store any quarter, month, or week week number in any database, I use integer values in formats YYYYQQ, YYYYMM, or YYYYWW. It ensures those values can always to be ordered properly, and are valid for any regional settings (except week numbers, as those depend on specific region). I.e.
    202504 instead of "Apr 2025",
    202402 instead of "Feb 2024",
    202503 instead of "Mar 2025".

    Also at workplace I had a calendar table available (not anymore currently, as I am on pension now, and haven't created one at my home computer jet), with date as PK field, and various additional fields like those quarter, month and week numbers, but also fields for indicating state holidays (0 or 1), or workdays (0 or 1), or fields containing normative working hours, or field containing number of days to current or previous workday (0 or a number), or field containing number of days to current or next workday (0 or a number), etc. You also can have there a field(s) which contains date texts like those you currently have (e.g. "Apr 2025").
    The calendar table contained all dates from some starting one and into some number of years into future. All additional fields were stored as values, and I had a procedure to add new dates and values to calendar table whenever there was a need for this.
    In case the database using the calendar table was in local LAN only, the table was stored in calendar database available to any database in same LAN, and linked to database using it, or queried from calendar database. In case the database was sent to some outside user, or the link to calendar table was not guaranteed for some reason, a copy of calendar table was added into database.
    This calendar table is useful for any calculations for specific dates, or specific date periods, or for creating reports, etc.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Clarify. First you say it is an Access table then you say field is linked to an Excel sheet. This makes no sense. This "table" is a linked worksheet?

    And input mask in what textbox where?

    If these values are treated as text by Access, need to convert to number values. Parse the string into its date parts using string manipulation functions. Might want to build a custom function that does this and returns YYYYMM number.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    @June7

    CDate() will do the job in one function.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Oh, that's right. Didn't realize could convert just mon year string.

    ORDER BY CDate(fieldname)

    "Apr 2025" becomes 4/1/2025
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    Gustav's Avatar
    Gustav is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    32
    Convert to date values and order by these:
    Code:
    Select * From YourTable 
    Order By CDate([YourTextMonthYearField]) Desc

  13. #13
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Quote Originally Posted by Welshgasman View Post
    I did not know Excel had input masks?
    The input mask is in access not in Excel

  14. #14
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Thank you all.
    Yes the CDate() Solved the issue.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 01-16-2019, 02:13 PM
  2. Replies: 7
    Last Post: 05-25-2018, 05:56 PM
  3. Replies: 1
    Last Post: 04-07-2014, 02:59 AM
  4. sort filter, doesn't sort everything
    By deso in forum Queries
    Replies: 4
    Last Post: 01-25-2013, 05:27 AM
  5. How to sort by three sort orders
    By captgnvr in forum Access
    Replies: 4
    Last Post: 11-09-2009, 07:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums