Results 1 to 4 of 4
  1. #1
    justauser is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    15

    Showing null values when calculating days passed

    I am using workdays to calculate time passed between two dates. I also have a table for Holidays that I don't want counted as work days. This is working well, but I would like my queries to show a null value instead of showing #Error when a date field is empty. Here is my coding in my Module.

    Option Compare Database


    Option Explicit


    Public Function Workdays(ByRef startDate As Date, _
    ByRef endDate As Date, _
    Optional ByVal strHolidays As String = "Holidays" _
    ) As Integer
    ' Returns the number of workdays between startDate
    ' and endDate inclusive. Workdays excludes weekends and
    ' holidays. Optionally, pass this function the name of a table
    ' or query as the third argument. If you don't the default
    ' is "Holidays".
    On Error GoTo Workdays_Error
    Dim nWeekdays As Integer
    Dim nHolidays As Integer
    Dim strWhere As String

    ' DateValue returns the date part only.
    startDate = DateValue(startDate)
    endDate = DateValue(endDate)

    nWeekdays = Weekdays(startDate, endDate)
    If nWeekdays = -1 Then
    Workdays = -1
    GoTo Workdays_Exit
    End If

    strWhere = "[Holiday] >= #" & startDate _
    & "# AND [Holiday] <= #" & endDate & "#"

    ' Count the number of holidays.
    nHolidays = DCount(Expr:="[Holiday]", _
    Domain:=strHolidays, _
    Criteria:=strWhere)

    Workdays = nWeekdays - nHolidays

    Workdays_Exit:
    Exit Function

    Workdays_Error:
    Workdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Workdays"
    Resume Workdays_Exit
    End Function


    Public Function Weekdays(ByRef startDate As Date, _
    ByRef endDate As Date _
    ) As Integer
    ' Returns the number of weekdays in the period from startDate
    ' to endDate inclusive. Returns -1 if an error occurs.
    ' If your weekend days do not include Saturday and Sunday and
    ' do not total two per week in number, this function will
    ' require modification.
    On Error GoTo Weekdays_Error

    ' The number of weekend days per week.
    Const ncNumberOfWeekendDays As Integer = 2

    ' The number of days inclusive.
    Dim varDays As Variant

    ' The number of weekend days.
    Dim varWeekendDays As Variant

    ' Temporary storage for datetime.
    Dim dtmX As Date

    ' If the end date is earlier, swap the dates.
    If endDate < startDate Then
    dtmX = startDate
    startDate = endDate
    endDate = dtmX
    End If

    ' Calculate the number of days inclusive (+ 1 is to add back startDate).


    varDays = DateDiff(Interval:="d", _
    date1:=startDate, _
    date2:=endDate) + 1

    ' Calculate the number of weekend days.
    varWeekendDays = (DateDiff(Interval:="ww", _
    date1:=startDate, _
    date2:=endDate) _
    * ncNumberOfWeekendDays) _
    + IIf(DatePart(Interval:="w", _
    Date:=startDate) = vbSunday, 1, 0) _
    + IIf(DatePart(Interval:="w", _
    Date:=endDate) = vbSaturday, 1, 0)

    ' Calculate the number of weekdays.
    Weekdays = (varDays - varWeekendDays)

    Weekdays_Exit:
    Exit Function

    Weekdays_Error:
    Weekdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Weekdays"
    Resume Weekdays_Exit
    End Function

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you tried using the IsDate() function?

  3. #3
    justauser is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    15
    I'm not quite sure what you mean. Would I use that function under my dates fields, or under the calculated date field?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You would use it to verify the incoming dates to your Workdays() function.

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

Similar Threads

  1. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  2. Replies: 4
    Last Post: 09-06-2011, 02:20 PM
  3. Not showing records with null sum
    By eww in forum Queries
    Replies: 3
    Last Post: 04-04-2011, 03:10 PM
  4. Calculating null fields in a form
    By chu3w in forum Forms
    Replies: 1
    Last Post: 02-26-2010, 02:00 PM
  5. Null Values not showing up in a Query
    By Valli in forum Queries
    Replies: 0
    Last Post: 01-04-2006, 03:53 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