Results 1 to 6 of 6
  1. #1
    roar58 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Kristiansand, Norway
    Posts
    32

    The period og days between two dates

    I want an integer in return which expresses the number of days between two dates. Ex: First date: Sept 30, 2013 Second date:October 2, 2013 - will return "3".
    Hope to get some help with this!
    Roar, Norway

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    Investigate DateDiff function. Access Help has guidelines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    roar58 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Kristiansand, Norway
    Posts
    32
    Thank you - for putting me on the right trace - I made this formula: =(DateDiff("d";[FromDt];[ToDt])+1)
    But I don't want all the days included - only the work days, e.g.: Mon Tue Wed Thu Fri - and not Sat and Sun. I discovered an intverval code "w" for the six weekdays, but didn't find any code for work days. Can I still use this DateDiff function or do I have to use another function?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    That will require your own User Defined Function (UDF). Here's what I use if there is no Holiday table:
    Code:
    Public Function NetWorkdays(dteStart As Date, dteEnd As Date) As Integer
       '-- Returns a negative number if dteStart is > dteEnd
       Dim intGrossDays As Integer
       Dim dteCurrDate As Date
       Dim i As Integer
       intGrossDays = Abs(DateDiff("d", dteStart, dteEnd))
       NetWorkdays = 0
       For i = 0 To intGrossDays
          dteCurrDate = dteStart + i
          If Weekday(dteCurrDate, vbMonday) < 6 Then
             NetWorkdays = NetWorkdays + 1
          End If
       Next i
       If dteStart > dteEnd Then
          NetWorkdays = NetWorkdays * -1
       End If
    End Function

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    BTW, UDF's need to be in a Standard Module and not in a Form's module.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    As RG noted, that code won't handle holidays. If you need to consider holidays, will need a table of holidays and modify code. This is common topic in forum and many code examples on web.

    Put function in standard module if want to be able to access it from multiple locations, otherwise UDF's can be placed behind forms and reports. They just would be available only to the form or report they are behind.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. To find Month and Days in given date period
    By waqas in forum Programming
    Replies: 1
    Last Post: 02-12-2013, 02:50 PM
  2. Replies: 4
    Last Post: 01-25-2013, 05:20 AM
  3. Dates before 30 days
    By fabiobarreto10 in forum Queries
    Replies: 7
    Last Post: 04-20-2012, 12:11 PM
  4. Dates & Days Fields
    By djclntn in forum Database Design
    Replies: 5
    Last Post: 10-22-2011, 06:22 PM
  5. Select Dates between days and two days ago?
    By rstonehouse in forum Queries
    Replies: 4
    Last Post: 08-18-2010, 02:13 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