I am "writing" a boolean function in VBA (Access 2003)to test whether a date entered by user is within the financial year that a project is planned for - the financial year runs from 1 July YYYY to 30 June YYYY. The function parses the project's ID code (called "Ref")to determine the project's financial year, then sets the start and end dates of that year to test whether the date entered is between them. "Ref" is a string in a standard form in which the first 7 characters represent the financial year:"2009-10:Aggregation" . The function has two arguments: "Ref" and "DateEntere d" (as date data type). I have used the CDate() function to ensure that the dates are actually date data type and Isdate() function to test that that has worked. As the users will enter British date formats, I have formatted the entered dates to US format to avoid the known issues with dates in Access VBA. Unfortunately the code is returning inaccurate and apparently random results so I would appreciate any comments on the following code:
When I test this function in the code editor with
?fnProjectFY("2 009-10:Aggregation" , #5/12/2010#)
debug window prints the following result:
True
Should be FALSE as 5 December 2010 is not within the prescribed financial year - aaaaargh!
Code:
Public Function fnProjectFY(Ref As String, DateEntered As Date) As Boolean Dim varYearStart, varYearEnd As Variant On Error Resume Next varYearStart = "01 July " & Left(Ref, 4) varYearStart = CDate(varYearStart) varYearStart = Format(varYearStart, "mm/dd/yyyy") varYearEnd = "30 June " & Left(Ref, 2) & Mid(Ref, 6, 2) varYearEnd = CDate(varYearEnd) varYearEnd = Format(varYearEnd, "mm/dd/yyyy") If DateEntered >= varYearStart And DateEntered <= varYearEnd Then fnProjectFY = True Else fnProjectFY = False End If End Function
?fnProjectFY("2 009-10:Aggregation" , #5/12/2010#)
debug window prints the following result:
True
Should be FALSE as 5 December 2010 is not within the prescribed financial year - aaaaargh!
Comment