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.

[sourcecode language=’VB’]
‘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

exitHere:
Exit Function

errHandler:
MsgBox err.Description
Resume exitHere

End Function
[/sourcecode]

So I called the function as shown below and got the weird results. Can you spot the not??

debugWindow

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.

debugWindowCorrect

So please pay attention or take a break if you feel like your concentration level is going down.

Till next time, yours truly.

Bug caused by not paying attention.
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *