Insert a blank line after the If...End If statement.
3. Add the following code:
MsgBox "Birth Year after If: " & CStr(iReturn)
Your code should now look like Listing 7-2.
Listing 7-2. BirthYear Function with MsgBox Debugging
Function BirthYear(Age As Integer, HadBDay As Boolean)
Dim iReturn As Integer
Dim iCurrYear As Integer
iCurrYear = Year(Date)
iReturn = iCurrYear = Age
MsgBox "Current Year: " & CStr(iCurrYear)
MsgBox "Birth Year before If: " & CStr(iReturn)
If Not HadBDay Then
iReturn = iReturn - 1
End If
MsgBox "Birth Year after If: " & CStr(iReturn)
BirthYear = iReturn
End Function
We??™re checking to see that the Year function is returning the correct value, and we??™re
checking our return value before and after the If...End If statement to see if the code fell
into it and possibly changed there.
4. Run the code in the Immediate window, clicking OK at each message box.
Figures 7-7, 7-8, and 7-9 show us that our current year value looks good but the iReturn
value has a problem. The problem must lie in our logic.
Figure 7-7. Current Year is correct.
CHAPTER 7 n DEBUGGING AND ERROR HANDLING 256
Figure 7-8. The iReturn variable is incorrect before the If...End If statement.
Figure 7-9. The iReturn variable did not fall into the If...End If statement, and it is still incorrect.
Now we know we??™ve got an issue at the point in the code where we set the iReturn variable
value. Let??™s take a look at that line of code:
iReturn = iCurrYear = Age
It??™s fairly obvious at this point, but instead of subtracting the age from the year, this code
is creating a conditional statement setting iReturn to True or False if the year equals Age.
Pages:
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246