Results 1 to 11 of 11
  1. #1
    teirrah1995 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    35

    How to use DateDiff?


    Hey. I'm awful at using online tutorials, unless I find an example which is almost exactly what I want to do, so could you help me understand DateDiff?

    I have a field, Recall_Date. I've made a query, and I need to find everyone whose recall date is in one month's time from the current day.

    Thanks for any help.

  2. #2
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    Does it need to be exactly one month from the current day or just within one month?

  3. #3
    teirrah1995 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    35
    Well, it will eventually be for reminder letters which would either be printed off every morning, meaning they need to be the exact day, or at the beginning of the week, meaning they have to be during a week that's a month away. The every day option would be simpler though, I'm guessing.
    Thanks.

  4. #4
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    I think the DateAdd function would be better suited for what you are trying to do. Set the criteria for Recall_Date in your query to:
    Code:
    DateAdd("m", -1, Date)
    If you would prefer to send them out for a week's worth at a time you could use something like this:

    Code:
    Between DateAdd("m",-1,Date()) And DateAdd("ww",+1,DateAdd("m",-1,Date()))

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by teirrah1995 View Post
    Hey. I'm awful at using online tutorials, unless I find an example which is almost exactly what I want to do, so could you help me understand DateDiff?

    I have a field, Recall_Date. I've made a query, and I need to find everyone whose recall date is in one month's time from the current day.

    Thanks for any help.
    also, here are more absolutes for you to browse through: http://www.ajenterprisesonline.com/_dates

  6. #6
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    Quote Originally Posted by slave138 View Post
    I think the DateAdd function would be better suited for what you are trying to do. Set the criteria for Recall_Date in your query to:
    Code:
    DateAdd("m", -1, Date)
    If you would prefer to send them out for a week's worth at a time you could use something like this:

    Code:
    Between DateAdd("m",-1,Date()) And DateAdd("ww",+1,DateAdd("m",-1,Date()))
    I don't think he want 7 days from a month ago, he was wondering in choosing a week ago or a month ago, then he pick a month ago.

    And we must be careful that if we want only one day in one month ago, using dateadd("m",-1,date) will miss the 31st day of last month if this month has only 30 days, e.g. we can never catch (Jan 29,) Jan 30 and Jan 31 by using dateadd("m",-1,date).

  7. #7
    teirrah1995 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    35
    Quote Originally Posted by weekend00 View Post
    And we must be careful that if we want only one day in one month ago, using dateadd("m",-1,date) will miss the 31st day of last month if this month has only 30 days, e.g. we can never catch (Jan 29,) Jan 30 and Jan 31 by using dateadd("m",-1,date).
    That's annoying. Would that be any better if I say 4 weeks, or 30 days?
    Also, it's she ^^

    Thanks for the replies.

  8. #8
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    Quote Originally Posted by teirrah1995 View Post
    That's annoying. Would that be any better if I say 4 weeks, or 30 days?
    Also, it's she ^^

    Thanks for the replies.
    Weeks or Days should be less prone to problems. Just to clarify your original goal, are you ideally trying to get a list of results for an entire week going back x amount of time?

    ...at the beginning of the week, meaning they have to be during a week that's a month away

  9. #9
    teirrah1995 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    35
    Well, the whole thing's going forward in time. And yes, ideally, it would be for a whole week so you only have to print of the letters on, say, Monday, rather than everyday.

  10. #10
    slave138's Avatar
    slave138 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2010
    Location
    WI
    Posts
    233
    I added a module with the LastMonday function:
    Code:
    Public Function LastMonday(dDate As Date) As Date
        'if today is Monday, then return today's date
        If Weekday(dDate, vbSunday) = 2 Then
            LastMonday = dDate
        Else
            LastMonday = dDate - Weekday(dDate, vbSunday) + 2
        End If
    End Function
    (Source)

    Then in the date field of the query I used this as the criteria:
    Code:
    Between DateAdd("ww",4,LastMonday(Date())) And DateAdd("d",6,DateAdd("ww",4,LastMonday(Date())))
    This method allows you to process the data any time during the week (in case someone forgets to do it Monday) by using the last Monday as the base date. It then returns all records between 4 weeks and the 6 days following that from the base day.

    You can try it out in the attached sample.

  11. #11
    teirrah1995 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Sep 2010
    Posts
    35
    Wow, that's perfect! And to change the number of weeks, I just replace the two '4's in the second code to whatever? Thank you!

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

Similar Threads

  1. Need Help with Datediff
    By gonzod in forum Access
    Replies: 5
    Last Post: 08-26-2010, 02:29 PM
  2. Datediff
    By greggue in forum Queries
    Replies: 2
    Last Post: 08-13-2010, 03:53 PM
  3. Change Between by DateDiff or DateAdd.
    By wagner.bts in forum Queries
    Replies: 1
    Last Post: 06-05-2009, 11:24 AM
  4. DateDiff function
    By Scott R in forum Reports
    Replies: 5
    Last Post: 12-03-2008, 07:32 AM
  5. Datediff() help needed
    By geoff44 in forum Access
    Replies: 2
    Last Post: 11-20-2008, 04:44 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