Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43

    DateDiff

    I have an expression for my query, DateDiff("yyyy",[DOB],now())


    This works to a point, if DOB has not happened this year yet, than the years are off by one year. is there a way to correct this problem?
    TIA to all

  2. #2
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    There are plenty (And i mean plenty) of sites on the internet that have this question asked loads of times... (Because i searched for it myself).

    I won't link you sites, as i'm sure you are able to google it... However i will link this:
    Note i've been using it in SQL Server, however it should give you an idea about how to solve the problem...

    Code:
    
    ALTERFUNCTION [dbo].[GetAge](@DOB datetime, @Today Datetime)RETURNSInt
    AS
    Begin
    Declare @Age AsInt
    Set @Age =Year(@Today)-Year(@DOB)
    IfMonth(@Today)<Month(@DOB)
    Set @Age = @Age -1
    IfMonth(@Today)=Month(@DOB)andDay(@Today)<Day(@DOB)
    Set @Age = @Age - 1
    Return @Age
    End
    
    Good luck.

  3. #3
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    Thanks Rixxe, I will give it a try
    Marty

  4. #4
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    This might be useful for you:

    http://www.everythingaccess.com/tuto...-given-the-DOB

    Provided by Allen Browne


    Good luck

  5. #5
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    Again want to say thanks, is there a code for year, month, and day? so I could extend the function to include the years and months, and days?

  6. #6
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    So you want to know the days and months, between DOB and Today() for example?

    I'm sure if you have a go, you might be able to add it in to the query.

    This might give you an idea:

    DateDiff("yyyy",tbl.DOB,tbl.Date)+Int(Format(tbl.D ate,"mmdd")<Format(tbl.DOB,"mmdd")) AS Age

  7. #7
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    Thanks Rixxe, I will try to work it in some how?
    Just to say THANKS again, just taking the time to help me.
    Marty

  8. #8
    websterh is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    8
    I have found this code to be useful too.

    =DateDiff("yyyy",[DOB],Now())+Int(Format(Now(),"mmdd")<Format([DOB],"mmdd"))

    I've used this and tested its accuracy and it updated the age when i entered a date -1 day and == to the DOB

  9. #9
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    It works as you said. the year...
    I have tried the year, month and day code and I can't get it to work, I'm a novice at this, it looks like I will need a complete code and expression that already works. Can you help? or can you send me to a URL that has it, I've tried with no luck.

    Again thanks Rixxe

  10. #10
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Well you are in luck (Hopefully!)

    This site (Thanks to RG) gives a great example:
    http://www.mvps.org/access/datetime/date0001.htm

    You want to find the age of someone at a certain date, Is the certain date always TODAY() ?
    I'll see what i can find out to help you... hopefully someone else might have come across the problem before.

    EDIT: Site for calculating age in years and months:
    http://www.tek-tips.com/faqs.cfm?fid=5876

  11. #11
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43

    Wink Marty

    As I said the year expression you sent works fine just as you thought.
    Now I'm trying to complete what I really want and hope you can or someone can help.

    A function that is complete, with the expression that goes with it. Year, Month, Day. I can modify it with my [DOB] inplace of ?. I'm really a total novice at this but i'm trying.

    All HELP Welcome
    mrkaye

  12. #12
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Ok, well the [DOB] is the a date of birth field.
    So you can change '[DOB]' to '2010/01/01' if you only want to see 1 result for all records (If they all have the same DOB, or you can change it to another field.)

    You can also change the 'Now()' part of the code, Now() or Today() represents todays date, so you could shove a field in there if you wish, or again '2010/01/01' etc etc

    Now as far as i know, when using DateDiff() You can only specify the following:
    Year
    YY
    DD
    MM
    YYYY
    etc etc
    Never tired just selecting DateDiff(yyymmdd, ......,.....)

  13. #13
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43

    Marty

    Rixxe I can change the DOB field to what ever, and I dont need a datediff function. any function that will give me "like 35 years 7 months 5 days" based on the date of birth, I would also need the expression that goed with the function. if its possible.
    TIA
    Marty

  14. #14
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    After searching for a long time, i found this:
    You will have to alter it, as it's supposed to be displayed differently from how you want it.
    Alas i don't work with VBA enough to help you with that, so hope this is useful for you...

    Code:
    Function fAgeYMD(startdate As Date, EndDate As Date) As String
    'Purpose:   Returns the difference between StartDate and EndDate in full years, months and days
    'Coded by:  raskew
    'To call:
    ' ? fAgeYMD(#7/6/54#, #10/3/84#)
    'Returns:
    ' 30 years 2 months 28 days
     
    Dim intHold As Integer
    Dim dayHold As Integer
     
       intHold = Int(DateDiff("m", startdate, EndDate)) + _
                 (EndDate < DateSerial(year(EndDate), month(EndDate), day(startdate)))
     
       If day(EndDate) < day(startdate) Then
          dayHold = DateDiff("d", startdate, DateSerial(year(startdate), month(startdate) + 1, 0)) + day(EndDate)
       Else
          dayHold = day(EndDate) - day(startdate)
       End If
     
       fAgeYMD = Int(intHold / 12) & " year" & IIf(Int(intHold / 12) <> 1, "s ", " ") _
                 & intHold Mod 12 & " month" & IIf(intHold Mod 12 <> 1, "s ", " ") _
                 & LTrim(str(dayHold)) & " day" & IIf(dayHold <> 1, "s", "")
     
    End Function

    Good luck



    (Note: This is not my code)

  15. #15
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43

    marty

    Rixxe I will try to modify this function, it looks like it would do the job?

    I really appreciate what you must have gone thru.

    I will let you know what happens.
    TIA
    Marty

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. DateDiff
    By ROB in forum Access
    Replies: 2
    Last Post: 10-30-2010, 03:58 AM
  2. How to use DateDiff?
    By teirrah1995 in forum Queries
    Replies: 10
    Last Post: 10-19-2010, 12:07 PM
  3. Need Help with Datediff
    By gonzod in forum Access
    Replies: 5
    Last Post: 08-26-2010, 02:29 PM
  4. Datediff
    By greggue in forum Queries
    Replies: 2
    Last Post: 08-13-2010, 03:53 PM
  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