I recently wrote the function below in MS Access VBA. I have a string that represent a year formatted as yyyy/yy and the function should return the string as yyyy-yyyy. It makes use of the Access Mid function that gets a substring of a text from any position. So here’s the function.
‘returns the year in format 2010-2011
Public Function FormatYear(ByVal theYear As String) As String
On Error GoTo errHandler
Dim first, last, result As String
first = Mid(theYear, 1, 4)
last = Mid(theYear, 6, 2)
result = first & “-20” & last
FormatYear = result
So I called the function as shown below and got the weird results. Can you spot the not??
Yeah. The problem was that I supplied the year to the function as a string but without the quotation marks. Like 2010/11 instead of “2010/11” . And that was the cause of the weird results. I guess I was tired.
So after a cup of coffee and a 10 minute break I realized the bug and as you can see below, voila.
So please pay attention or take a break if you feel like your concentration level is going down.
Till next time, yours truly.