Results 1 to 4 of 4
  1. #1
    AminSubhani is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Location
    Sahiwal
    Posts
    1

    Adding Date Difference

    Dear sir,


    I own my business in different cities. My employs keep on transferring from one city to the other.
    I want to calculate length "years, months and days" of employs in a city and then calculate total service an employ in all the cities. Answer should be in "years, months and days". Moreover, the present stay of an employ must be continued i.e with each passing day it should automatically add on.
    Thanks

    AminSubhani

    Required OUTPUT
    Name City From To Service Total Service
    Mansoor Lahore 1 Jan 2007 31 Dec 2007 1 year 0 months 0 Days
    Mansoor Multan 1 Jan 2008 15 Apr 2010 2 Years 3 Months 15 Days
    * Mansoor Faisal Abad 16 Apr 10 & 12 Nov 15 (Continue) 5 Years 6 Months 27 days 8 Years 10 Months 12 Days
    * The last entry should be based on current date (&) i.e today is 12 Nov 15 and answer is 5 Years 6 Months 27 days with Total Service of 8 Years 10 Months 12 Days but tomorrow i.e on 13 Nov 15 it should be automatically 5 Years 6 Months 28 days with Total Service of 8 Years 10 Months 13 Days and so on.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Why not simply use DaysInEmploy? Why Years, months and days?
    If you need to format it for display, then you could have a routine to do so.

    Bob 2312 days
    Jim 345 days

    If you know startDate and EndDate, you could use DateDiff.
    Just a thought for consideration.

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    the calculation for years/months/days is a common request and is handled in a UDF (i.e. written in vba code) - here is an example

    https://bytes.com/topic/access/answe...etween-2-dates

    but google/bing 'vba years,months,days' or similar to find more

    with regards your query, you have shown the result you want but not the underlying data that will be used to populate it. Please provide some sample data. Note that Name, From, To are all reserved words and using them as field names will only cause problems, so if these really are your field names, change them

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is a query that seems to return the y,m,d :

    Of course, you have to change the table name and field names to match yours. To get the total, I would use a totals query or use grouping in a report.
    Code:
    SELECT tblAminSubhani.EmpName, tblAminSubhani.City, tblAminSubhani.startdate, tblAminSubhani.EndDate, DateDiff("yyyy",[startdate],Nz([EndDate],Date())+1)-IIf(Format([StartDate],"mmdd")>Format(Nz([EndDate],Date()),"mmdd"),1,0) AS theYears, IIf(Day([startdate])<=Day(Nz([EndDate],Date())+1),DateDiff("m",[startdate],Nz([EndDate],Date())+1)-[theYears]*12,DateDiff("m",[startdate],Nz([EndDate],Date()+1))-[theYears]*12-1) AS theMonths, DateDiff("d",DateAdd("m",[theMonths],DateAdd("yyyy",[theYears],[startdate])),Nz([EndDate],Date())+1) AS theDays, [theyears] & " years, " & [themonths] & " months, " & [thedays] & " days" AS ToString
    FROM tblAminSubhani;

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

Similar Threads

  1. Difference between the value from two different date
    By mahmud1180 in forum Programming
    Replies: 2
    Last Post: 10-02-2014, 08:40 AM
  2. Date Difference Not Quite Right
    By StevenCV in forum Access
    Replies: 11
    Last Post: 01-30-2014, 08:12 AM
  3. Date difference
    By Kaaivin in forum Queries
    Replies: 2
    Last Post: 07-03-2013, 06:06 AM
  4. Date Difference from same column
    By akmehsanulhaque in forum Queries
    Replies: 1
    Last Post: 02-19-2013, 01:31 PM
  5. Replies: 1
    Last Post: 02-12-2013, 03:48 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