1

I am trying to use the function in Access 2007.

I am getting a compile error - "There was an error compiling this function. The Visual Basic module contains a syntax error. DATEADD("w",2,[MyDateField])

I am not trying to use any VBA just adding 2 weekdays to a field in a query - Any help would be appreciated

2
  • Please post your code and tell what your are trying to achieve. Commented Jul 22, 2022 at 11:54
  • I just want to add 2 working days (weekdays) to the field ''' [mydatefield] ''' in a standalone query - the query is DATEADD("w",2,[MyDateField]) Commented Jul 22, 2022 at 12:04

1 Answer 1

1

DateAdd can only add days, not workdays. For that, a custom function is needed.

In a query, you can use my function, VDateAddWorkdays:

Select *, VDateAddWorkdays(2, [MyDateField]) As Date2
From YourTable

The function:

' Adds Number of full workdays to Date1 and returns the found date.
' Number can be positive, zero, or negative.
' Optionally, if WorkOnHolidays is True, holidays are counted as workdays.
' Returns Null if any parameter is invalid.
'
' For excessive parameters that would return dates outside the range
' of Date, either 100-01-01 or 9999-12-31 is returned.
'
' Will add 500 workdays in about 0.01 second.
'
' Requires table Holiday with list of holidays.
'
' 2015-12-19. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function VDateAddWorkdays( _
    ByVal Number As Variant, _
    ByVal Date1 As Variant, _
    Optional ByVal WorkOnHolidays As Boolean) _
    As Variant
    
    Dim ResultDate      As Variant
    
    ResultDate = Null
    
    If IsDateExt(Date1) Then
        If IsNumeric(Number) Then
            On Error Resume Next
            ResultDate = DateAddWorkdays(CDbl(Number), CDate(Date1), WorkOnHolidays)
            On Error GoTo 0
        End If
    End If
    
    VDateAddWorkdays = ResultDate
    
End Function

As you can see, it takes advantage of some helper functions and a table holding holidays if those are to be taken into account as well.

Too much code to post here - the functions can be found in my project at GitHub: VBA.Date.

The specific modules needed will be:

  • DateBase
  • DateCalc
  • DateFind
  • DateWork
  • VDateWork
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.