Results 1 to 13 of 13
  1. #1
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136

    Select receipts from last month


    I'm struggling with DateAdd and DatePart and SerialDate!! In a query I want to select all records of gifts received in the previous calender month. My field is 'Date Paid'. Can someone please string together what I need? Would there be a problem if this is run in January? Does the new year create any problems?

  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,641
    Try these, using Date() instead of dte:

    'First of previous month
    '-------------------------------------
    DateSerial(Year(dte), Month(dte) - 1, 1)

    'Last of previous month
    '-------------------------------------
    DateSerial(Year(dte), Month(dte), 0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    Thanks for the steer. I appreciate that. I may have a follow-up question but if so I will post it later.

  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,641
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    If I use >=DateSerial(Year(Date()),Month(Date())-1,1) in a query to select all entries for the last calendar month how will this work at the beginning of January? Will the Year(Date()) be interpreted as the current year and as a result will not select any records from December? If so, how do I overcome this problem?

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I think you are after something like this:

    dateserial(year(dateadd("m",-1,date())), month(dateadd("m",-1,date())),1)

  7. #7
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    Thanks for the guidance. That continues to do what I want it to do this month so I expect it will solve my problem in January. Thanks again.

  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,641
    The formulas were fine as they were:

    ?DateSerial(Year(#1/15/13#), Month(#1/15/13#) - 1, 1)
    12/1/2012
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    Apart from not understanding what "#1/15/13#" represents in your suggestion I'm not clear how the year element of the DateSerial formula can change to year-1 if it is being used in January. Maybe my understanding of the elements in the formula is flawed? Does it not look at the 3 elements of the current date and select the current year (unless there is a '-1' in which case it would select the previous year?

  10. #10
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    The #1/15/13# is just a fixed date in place of the date() function (date values have to be delimited with # signs in Access); Paul was illustrating that a date in January will yield the first of the December with the previous year. I too was surprised that having the-1 for only in the month term impacted the year as well. I will have to read up more on the DateSerial() function.

    Thanks Paul for the new knowledge; it will come in handy in the future.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problem. DateSerial is good that way; note the way you can use a Day argument of 0 to get the last day of the previous month, irregardless of how many days are in that month.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    Thanks to both of you. Sometimes logic is a bad thing if you don't fully understand the whole concept!!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problemo!
    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. Replies: 5
    Last Post: 07-26-2012, 12:59 PM
  2. Month/Month to date
    By Tony McGuire in forum Queries
    Replies: 9
    Last Post: 09-20-2011, 11:54 AM
  3. Track capacity changes month by month
    By Nils in forum Database Design
    Replies: 1
    Last Post: 02-18-2011, 03:03 PM
  4. Replies: 5
    Last Post: 11-15-2010, 06:12 PM
  5. Replies: 7
    Last Post: 07-19-2010, 08:55 AM

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