Results 1 to 14 of 14
  1. #1
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92

    Question Sort by date and year

    I have a query that feed a report and the format of the field is mmm yyyy.



    I was trying to sort by Month because it was sorting by A to Z, and reading the forum I create a new field that is MM, so I can sort the number and that will sort the month. But now it got worst, since january 2011 comes above december 2010.

    How can I sort by real DATE?

    P.S.: The field mmm yyyy is a summary of several dates. In the table its a normal date field dd mm yyyy. So in the query its a summary by month.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'd create a new field in the query:

    Format(DateField, "yyyymm")

    which will sort properly. You can display the other format to the user.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    Quote Originally Posted by pbaldy View Post
    I'd create a new field in the query:

    Format(DateField, "yyyymm")

    which will sort properly. You can display the other format to the user.
    Thanks.
    But how do I change the way the user see? In the report I'm trying to change but no sucess.

    I also want to create now a query to show only this week records. How do I filter that? The only date criteria I know is today() and date between etc. But this week is possible?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would have 2 fields in the query, one with each format. In the report's Sorting and Grouping, use mine, but in the report field visible to the user, use yours.

    You can use a criteria using between:

    Between OneDate And AnotherDate

    replacing the 2 date references with the appropriate functions:

    http://www.pacificdb.com.au/Support/...kfunctions.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    Quote Originally Posted by pbaldy View Post
    I would have 2 fields in the query, one with each format. In the report's Sorting and Grouping, use mine, but in the report field visible to the user, use yours.
    Works perfectly!

    You can use a criteria using between:

    Between OneDate And AnotherDate

    replacing the 2 date references with the appropriate functions:

    http://www.pacificdb.com.au/Support/...kfunctions.htm
    I built this:

    Between ([Birthday]-Weekday([Birthday];2) +1) And ([Birthday]-Weekday([Birthday];1) +7)

    But the result is all records. Like there is no criteria.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You're basically comparing records against themselves. Try

    Between (Date() - Weekday(Date(), 2) + 1) And (
    Date() + (7 - Weekday(Date(), 2)))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    Quote Originally Posted by pbaldy View Post
    You're basically comparing records against themselves. Try

    Between (Date() - Weekday(Date(), 2) + 1) And (
    Date() + (7 - Weekday(Date(), 2)))
    Now it doesn't bring any records.
    What is Date() ? Today?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes; more accurately the system date from the computer. If this is a birthday field, you'd want to go about it differently, since you really only want to match on month and day, not year.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    Something is wrong. I tried to make a simple one, only with month:

    WHERE (Month([Birthday])=Month(Date());

    Worked. But then I did this:

    WHERE (Month([Birthday])=Month(Date()) And Weekday([Birthday])=Weekday(2));

    Should appear all records of the month that have weekday Monday, right? But it is appearing one record from Friday. Day 22 to be exact.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you're looking for birthdays in the next x days, this type of thing should work:

    WHERE (((DateSerial(Year(Date()),Month([Birthdate]),Day([Birthdate]))) Between Date() And DateAdd("d",7,Date())))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    I get "Data type mismatch in the expression"

    Birthdays in the next days will work fine, but this week birthdays is too hard to make?

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Are there any records with no birth date? That can cause that error. If so, you can wrap the birthday field in the Nz() function:

    Nz([Birthdate],0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Douglasrac is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Nov 2010
    Posts
    92
    Yes, there are records without birthday.

    Thank you! Once more, working like a charm.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Year to date sum
    By jzacharias in forum Database Design
    Replies: 6
    Last Post: 09-10-2010, 10:38 AM
  2. Use field value as the year in a date
    By Eveline in forum Queries
    Replies: 1
    Last Post: 03-11-2010, 10:58 AM
  3. Date manipulaton for Financial Year
    By Neil Bingham in forum Access
    Replies: 0
    Last Post: 02-15-2009, 11:24 AM
  4. Finding data between two date for any year
    By gemini2 in forum Access
    Replies: 4
    Last Post: 04-05-2006, 06:20 AM
  5. Replies: 1
    Last Post: 12-09-2005, 10:29 PM

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