Option Compare Database   'Use database order for string comparisons
Option Explicit

Function FiscalDate (CalDate As Variant) As Variant
          
    '
    ' This function takes a calendar date and converts it to the
    ' corresponding fiscal month/year (day is arbitrarily set to
    ' 1).  The constant FISCAL_YEAR_OFFSET determines how many
    ' months difference there is between calendar date and fiscal
    ' date;  a positive value indicates that you ADD that many months
    ' and a negative value indicates that you SUBTRACT that many
    ' months.  Example:  given a calendar date of 3/15/94, the
    ' following results would be obtained:
    '
    '           FISCAL_YEAR_OFFSET     FiscalDate(3/15/94)
    '           ==================     ===================
    '                  0                    3/1/94 (March '94 is the third
    '                                               month of fiscal '94)
    '                  1                    4/1/94 (March '94 is the fourth
    '                                               month of fiscal '94)
    '                  6                    9/1/94 (March '94 is the ninth
    '                                               month of fiscal '94)
    '                 10                    1/1/95 (March '94 is the first
    '                                               month of fiscal '95)
    '                 -6                    9/1/93 (March '94 is the ninth
    '                                               month of fiscal '93)
    '
          
    Const FISCAL_YEAR_OFFSET% = 6       ' July 1 is start of next fiscal year

    Dim varWorkDate As Variant

    ' Is the value passed to us a valid date?  If not, return a #NULL#
    If Not IsDate(CalDate) Then
        FiscalDate = Null
        Exit Function
    End If

    ' Find first day of month for the input date
    varWorkDate = DateSerial(Year(CalDate), Month(CalDate), 1)

    ' Add FISCAL_YEAR_OFFSET to get it to the fiscal year and month
    FiscalDate = DateAdd("m", FISCAL_YEAR_OFFSET, varWorkDate)

End Function

