Results 1 to 15 of 15
  1. #1
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121

    Format for rounding in a query when using a function with date fields

    I have the following function in a module. Now I need to add it to a query and round so that I get the number of months between two dates


    My query field is:

    Months to Release: GetDays360 ([TimesheetDate,[ReleaseDate])

    The query is just calculating the time between i.e., ReleaseDate 10/31/2022 TimesheetDate 9/6/2019 Result 43086
    what I expect to get is the number of months which is 38

    How do I write this around this function and fields?

  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,521
    I assume that's a custom function you're calling. Are you sure you're passing it 2 date values (not text)? What is the function, so we can double check what it's supposed to receive and return?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    why not just use the datediff function?

  4. #4
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    I am not using datediff because it differs from Days360 enough to make a difference and my customer requires we get as close to Days360 as possible. I actually had this working but not sure where I messed it up.
    Here is my function:
    Public Function GetDays360(TimesheetDate As Date, Release As Date)
    Days360 = ((Year(Release Date) - Year(Timesheet Date)) - 1) * 360 + ((12 - Month(Timesheet Date)) * 30) + (30 - Day(Timesheet Date)) + ((Month(Release Date) - 1) * 30) + (Day(Release Date))

    End Function

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    What are the variables in red. They don't appear to be declared in the function

    Days360 = ((Year(
    Release Date) - Year(Timesheet Date)) - 1) * 360 + ((12 - Month(Timesheet Date)) * 30) + (30 - Day(Timesheet Date)) + ((Month(Release Date) - 1) * 30) + (Day(Release Date))
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    ReleaseDate 10/31/2022
    TimesheetDate 9/6/2019

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    If you have embedded spaces in your variable names, you need to use/enclose with "[" and "]".
    eg [Release Date].

    Perhaps you should name the function MonthsXX or similar to show you expect Months to be returned.

    Eg using Datediff

    Code:
    Function fGetMonths(releaseDate As Date, TimesheetDate As Date) As Integer
        On Error GoTo fGetMonths_Error
        fGetMonths = DateDiff("m", releaseDate, TimesheetDate)
       
        On Error GoTo 0
    fGetMonths_Exit:
        Exit Function
    
    fGetMonths_Error:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fGetMonths, line " & Erl & "."
        GoTo fGetMonths_Exit
    End Function

  8. #8
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    I do not have embedded spaces

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I was basing my comment on your post #4 -specifically the code in blue??

    Here is my function:
    Public Function GetDays360(TimesheetDate As Date, Release As Date)
    Days360 = ((Year(Release Date) - Year(Timesheet Date)) - 1) * 360 + ((12 - Month(Timesheet Date)) * 30) + (30 - Day(Timesheet Date)) + ((Month(Release Date) - 1) * 30) + (Day(Release Date))

    End Function

  10. #10
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    here is what I have:

    Public Function GetDays360(ReleaseDate As Date, TimesheetDate As Date) As Integer
    GetDays360 = ((Year(ReleaseDate) - Year(TimesheetDate)) - 1) * 360 + ((12 - Month(TimesheetDate)) * 30) + (30 - Day(TimesheetDate)) + ((Month(ReleaseDate) - 1) * 30) + (Day(ReleaseDate))
    End Function

    ReleaseDate 10/31/2020
    TimesheetDate 9/6/2019

    Result I am getting is 1052
    Result I would like to get 38

    Help!

  11. #11
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    Apologies, ReleaseDate 11/29/2019

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    ???? I get 332 with your data and function. OOOOps I changed the wrong date.!!!!!!!

    UPDATE:
    Code:
    Sub testgetDays360()
    Dim rdate As Date: rdate = #11/29/2019#
    Dim tdate As Date: tdate = #9/6/2019#
    Debug.Print GetDays360(rdate, tdate)
    End Sub
    83

    In plain English, what do you think your function is doing??

  13. #13
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by smg View Post
    Apologies, ReleaseDate 11/29/2019
    Using your dates I get 443
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  14. #14
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Did you look at the update file I uploaded in response to your other post? https://www.accessforums.net/showthread.php?t=81516?

    Here is the function to get you the months:
    Code:
    Public Function GetMonths360(TimesheetDate As Date, Release As Date)
    GetMonths360 = ((Year([TimesheetDate]) - Year([Release])) - 1) * 360 + ((12 - Month([Release])) * 30) + (30 - Day([Release])) + ((Month([TimesheetDate]) - 1) * 30) + (Day([TimesheetDate]))
    GetMonths360 = -Int(-GetMonths360 / 30)
    End Function
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #15
    smg is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Apr 2019
    Posts
    121
    Thanks Vlad, I think that works!

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

Similar Threads

  1. Replies: 6
    Last Post: 04-06-2020, 04:31 AM
  2. Replies: 5
    Last Post: 09-16-2018, 04:58 PM
  3. Rounding function
    By michaelhare in forum Access
    Replies: 1
    Last Post: 03-16-2015, 12:55 AM
  4. Replies: 3
    Last Post: 10-19-2014, 06:33 PM
  5. Replies: 3
    Last Post: 08-24-2010, 09:26 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