Results 1 to 5 of 5
  1. #1
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43

    Question Date Calculation

    I have a database with many fields, one being birthday and anniversary dates, I now have created a form with some fields from the database, First Name, Last Name, Birthday and Anniversary. What i need help with is a Macro or Moduel to extract from the Birthday field and a second Macro or Module for the Anniversary field so i can have how old a person is and the second Macro or moduel how long they are married. i have no idea how to create a Macro or module or how to install the Macro or Module. I need for a end result of Age=25 Years 7 Months and 22 days if there is a birthday field with data in it just a example output will vary depending on input. same is true for the anniversary. Please help with, TIA
    That does not work. it gives back wrong number of years, if I was born 12/31/1975 I get the number 35 but thats not right, its still only 34 plus years I still need years, moths, days. to get what I need in a form.
    TIA mrkaye
    Last edited by mrkaye; 11-09-2010 at 02:08 PM.

  2. #2
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Hi,

    You will want to use the DATEDIFF function.

    Have a look at this video from YouTube, or just read the instructions from Microsoft.

    Cheers,

  3. #3
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    I have a database with many fields, one being birthday and anniversary dates, I now have created a form with some fields from the database, First Name, Last Name, Birthday and Anniversary. What i need help with is a Macro or Moduel to extract from the Birthday field and a second Macro or Module for the Anniversary field so i can have how old a person is and the second Macro or moduel how long they are married. i have no idea how to create a Macro or module or how to install the Macro or Module. I need for a end result of Age=25 Years 7 Months and 22 days if there is a birthday field with data in it just a example output will vary depending on input. same is true for the anniversary. Please help with, TIA
    That does not work. it gives back wrong number of years, if I was born 12/31/1975 I get the number 35 but thats not right, its still only 34 plus years I still need years, moths, days. to get what I need in a form.
    TIA mrkaye

  4. #4
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    This should work:

    Code:
    Public Function Diff2Dates(Interval As String, Date1 As Variant, Date2 As Variant, _
    Optional ShowZero As Boolean = False) As Variant
    
    'Author:    ) Copyright 2001 Pacific Database Pty Limited
    '           Graham R Seach MCP MVP gseach@pacificdb.com.au
    '           Phone: +61 2 9872 9594  Fax: +61 2 9872 9593
    '           This code is freeware. Enjoy...
    '           (*) Amendments suggested by Douglas J. Steele MVP
    '
    'Description:   This function calculates the number of years,
    '               months, days, hours, minutes and seconds between
    '               two dates, as elapsed time.
    '
    'Inputs:    Interval:   Intervals to be displayed (as a string).  Use:
    '           y = years
    '           m = months
    '           d = days
    '           h = hours
    '           n = minutes
    '           s = seconds
    '           Date1:      The lower date (see below)
    '           Date2:      The higher date (see below)
    '           ShowZero:   Boolean to select showing zero elements
    '
    'Outputs:   On error: Null
    '           On no error: Variant containing the number of years,
    '               months, days, hours, minutes & seconds between
    '               the two dates, depending on the display interval
    '               selected.
    '           If Date1 is greater than Date2, the result will
    '               be a negative value.
    '           The function compensates for the lack of any intervals
    '               not listed. For example, if Interval lists "m", but
    '               not "y", the function adds the value of the year
    '               component to the month component.
    '           If ShowZero is True, and an output element is zero, it
    '               is displayed. However, if ShowZero is False or
    '               omitted, no zero-value elements are displayed.
    '               For example, with ShowZero = False, Interval = "ym",
    '               elements = 0 & 1 respectively, the output string
    '               will be "1 month" - not "0 years 1 month".
    '
    'For example:
    '
    '?Diff2Dates("y", #06/01/1998#, #06/26/2002#)
    '4 years
    '?Diff2Dates("ymd", #06/01/1998#, #06/26/2002#)
    '4 years 25 days
    '?Diff2Dates("ymd", #06/01/1998#, #06/26/2002#, True)
    '4 years 0 months 25 days
    '?Diff2Dates("d", #06/01/1998#, #06/26/2002#)
    '1486 days
    '
    '?Diff2Dates("h", #01/25/2002 01:23:01#, #01/26/2002 20:10:34#)
    '42 hours
    '?Diff2Dates("hns", #01/25/2002 01:23:01#, #01/26/2002 20:10:34#)
    '42 hours 47 minutes 33 seconds
    '?Diff2Dates("dhns", #01/25/2002 01:23:01#, #01/26/2002 20:10:34#)
    '1 day 18 hours 47 minutes 33 seconds
    '
    '?Diff2Dates("ymd",#12/31/1999#,#1/1/2000#)
    '1 day
    '?Diff2Dates("ymd",#1/1/2000#,#12/31/1999#)
    '-1 day
    '?Diff2Dates("ymd",#1/1/2000#,#1/2/2000#)
    '1 day
    '
    
    
    On Error GoTo Err_Diff2Dates
    
       Dim booCalcYears As Boolean
       Dim booCalcMonths As Boolean
       Dim booCalcDays As Boolean
       Dim booCalcHours As Boolean
       Dim booCalcMinutes As Boolean
       Dim booCalcSeconds As Boolean
       Dim booSwapped As Boolean
       Dim dtTemp As Date
       Dim intCounter As Integer
       Dim lngDiffYears As Long
       Dim lngDiffMonths As Long
       Dim lngDiffDays As Long
       Dim lngDiffHours As Long
       Dim lngDiffMinutes As Long
       Dim lngDiffSeconds As Long
       Dim varTemp As Variant
    
       Const INTERVALS As String = "dmyhns"
    
    'Check that Interval contains only valid characters
       Interval = LCase$(Interval)
       For intCounter = 1 To Len(Interval)
          If InStr(1, INTERVALS, Mid$(Interval, intCounter, 1)) = 0 Then
             Exit Function
          End If
       Next intCounter
    
    'Check that valid dates have been entered
       If IsNull(Date1) Then Exit Function
       If IsNull(Date2) Then Exit Function
       If Not (IsDate(Date1)) Then Exit Function
       If Not (IsDate(Date2)) Then Exit Function
    
    'If necessary, swap the dates, to ensure that
    'Date1 is lower than Date2
       If Date1 > Date2 Then
          dtTemp = Date1
          Date1 = Date2
          Date2 = dtTemp
          booSwapped = True
       End If
    
       Diff2Dates = Null
       varTemp = Null
    
    'What intervals are supplied
       booCalcYears = (InStr(1, Interval, "y") > 0)
       booCalcMonths = (InStr(1, Interval, "m") > 0)
       booCalcDays = (InStr(1, Interval, "d") > 0)
       booCalcHours = (InStr(1, Interval, "h") > 0)
       booCalcMinutes = (InStr(1, Interval, "n") > 0)
       booCalcSeconds = (InStr(1, Interval, "s") > 0)
    
    'Get the cumulative differences
       If booCalcYears Then
          lngDiffYears = Abs(DateDiff("yyyy", Date1, Date2)) - _
                  IIf(Format$(Date1, "mmddhhnnss") <= Format$(Date2, "mmddhhnnss"), 0, 1)
          Date1 = DateAdd("yyyy", lngDiffYears, Date1)
       End If
    
       If booCalcMonths Then
          lngDiffMonths = Abs(DateDiff("m", Date1, Date2)) - _
                  IIf(Format$(Date1, "ddhhnnss") <= Format$(Date2, "ddhhnnss"), 0, 1)
          Date1 = DateAdd("m", lngDiffMonths, Date1)
       End If
    
       If booCalcDays Then
          lngDiffDays = Abs(DateDiff("d", Date1, Date2)) - _
                  IIf(Format$(Date1, "hhnnss") <= Format$(Date2, "hhnnss"), 0, 1)
          Date1 = DateAdd("d", lngDiffDays, Date1)
       End If
    
       If booCalcHours Then
          lngDiffHours = Abs(DateDiff("h", Date1, Date2)) - _
                  IIf(Format$(Date1, "nnss") <= Format$(Date2, "nnss"), 0, 1)
          Date1 = DateAdd("h", lngDiffHours, Date1)
       End If
    
       If booCalcMinutes Then
          lngDiffMinutes = Abs(DateDiff("n", Date1, Date2)) - _
                  IIf(Format$(Date1, "ss") <= Format$(Date2, "ss"), 0, 1)
          Date1 = DateAdd("n", lngDiffMinutes, Date1)
       End If
    
       If booCalcSeconds Then
          lngDiffSeconds = Abs(DateDiff("s", Date1, Date2))
          Date1 = DateAdd("s", lngDiffSeconds, Date1)
       End If
    
       If booCalcYears And (lngDiffYears > 0 Or ShowZero) Then
          varTemp = lngDiffYears & IIf(lngDiffYears <> 1, " years", " year")
       End If
    
       If booCalcMonths And (lngDiffMonths > 0 Or ShowZero) Then
          If booCalcMonths Then
             varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
                       lngDiffMonths & IIf(lngDiffMonths <> 1, " months", " month")
          End If
       End If
    
       If booCalcDays And (lngDiffDays > 0 Or ShowZero) Then
          If booCalcDays Then
             varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
                       lngDiffDays & IIf(lngDiffDays <> 1, " days", " day")
          End If
       End If
    
       If booCalcHours And (lngDiffHours > 0 Or ShowZero) Then
          If booCalcHours Then
             varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
                       lngDiffHours & IIf(lngDiffHours <> 1, " hours", " hour")
          End If
       End If
    
       If booCalcMinutes And (lngDiffMinutes > 0 Or ShowZero) Then
          If booCalcMinutes Then
             varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
                       lngDiffMinutes & IIf(lngDiffMinutes <> 1, " minutes", " minute")
          End If
       End If
    
       If booCalcSeconds And (lngDiffSeconds > 0 Or ShowZero) Then
          If booCalcSeconds Then
             varTemp = varTemp & IIf(IsNull(varTemp), Null, " ") & _
                       lngDiffSeconds & IIf(lngDiffSeconds <> 1, " seconds", " second")
          End If
       End If
    
       If booSwapped Then
          varTemp = "-" & varTemp
       End If
    
       Diff2Dates = Trim$(varTemp)
    
    End_Diff2Dates:
       Exit Function
    
    Err_Diff2Dates:
       Resume End_Diff2Dates
    
    End Function
    Cheers,

  5. #5
    mrkaye is offline Advanced Beginner
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Posts
    43
    I want to thank you, just need install and install
    mrkaye

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

Similar Threads

  1. Date Calculation
    By mrkaye in forum Forms
    Replies: 5
    Last Post: 11-11-2010, 01:10 AM
  2. Date Calculation
    By mrkaye in forum Forms
    Replies: 4
    Last Post: 11-10-2010, 10:42 AM
  3. Date Calculation ?
    By techexpressinc in forum Queries
    Replies: 2
    Last Post: 06-24-2009, 09:02 PM
  4. Date Calculation -- HELP!
    By klaauser in forum Forms
    Replies: 0
    Last Post: 12-22-2008, 02:14 PM
  5. Date Calculation within same fields
    By mslieder in forum Queries
    Replies: 3
    Last Post: 01-26-2006, 10:08 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