Share via

Microsoft Access: returning a value from a function to a subroutine

Anonymous
2018-12-12T15:45:58+00:00

I am attempting to create a function that will adjust an entered date so as to adjust for weekends or holidays.  What have I done incorrectly, that the adjusted date does not pass from the function to the subroutine?

Private Sub DATE_ENTERED_AfterUpdate()

    Me!DATE_ENTERED = DATE_ADJUST(Me!DATE_ENTERED)

End Sub

Public Function DATE_ADJUST(ByRef INITIAL_DATE As Date)

StartLoop:

    '/ if Saturday, add 2 days

        If Weekday(INITIAL_DATE) = 7 Then

            INITIAL_DATE = INITIAL_DATE + 2

        End If

    '/ if Sunday, add 1 day

        If Weekday(INITIAL_DATE) = 1 Then

            INITIAL_DATE = INITIAL_DATE + 1

        End If

    '/ if CAVC closing date, add 1 day and loop

        If Not (IsNull(DLookup("[CAVC closing date]", "[CAVC closing dates]", "[CAVC closing date] = #" & INITIAL_DATE & "#"))) Then

            INITIAL_DATE = INITIAL_DATE + 1

            GoTo StartLoop

        End If

End Function

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

ScottGem 68,830 Reputation points Volunteer Moderator
2018-12-12T15:51:30+00:00

After the End IF add:

DATE_ADJUST = INITIAL_DATE

You are not assigning the calculated date to the function, so there is nothing to return.

Also I would change the first line to:

Public Function DATE_ADJUST(ByRef INITIAL_DATE As Date) AS Date

To make sure what is returned is a Date/Time datatype.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-12-12T16:46:12+00:00

    The "missing piece" was the line of code after the End If.  THANK YOU!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-12-12T16:20:18+00:00

    Actually, it is possible to use a function like a sub. for example:

    Public Sub XMyDATE_ADJUST()

        Dim d As Date

        Dim e As Date

        e = Date   ' that is today

        d = MyDATE_ADJUST(e)   ' just add 1

        Debug.Print d, e

    End Sub

    '   output: 12:00:00 AM   12/13/2018

    Was this answer helpful?

    0 comments No comments