Note In the Immediate window when you precede a command or variable name with the character the result of the command or contents of the variable will be output to the Immediate window Otherwise the command will just run unless there are errors

We passed in 30 as the Age parameter (you didn't think I'd put my real age in there, did you?) and set the flag to True, indicating that the birthday had passed for this year. This code was run in the year 2007, so the result I would expect to see is 1977—but that's not what we see in Figure 7-5, is it?

Figure 7-5. Unexpected result in Immediate window

Let's check our variables and make sure they're holding correct values by inserting a few message boxes. You can create message boxes by calling the MsgBox function. As shown in Figure 7-6, the MsgBox function takes a few arguments, but since we're not presenting these to a user at this time, we'll just pass in the first argument, the prompt or message.


MsgBox(Prcnnpr. [Buttons ¿sVbMsgBoxStyle = vbOKOnly], [TVtte], [HelpHte], [Context]) As VbMsgBoxResult |

Figure 7-6. The MsgBox function's argument list

1. In the BirthYear function, add the following code above the If...End If statement:

MsgBox "Current Year: " & CStr(iCurrYear) MsgBox "Birth Year before If: " & CStr(iReturn)

2. 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.

Figure 7-7. Current Year is correct.

Birth Year before If: 0

Figure 7-8. The iReturn variable is incorrect before the If...EndIf 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.

5. Change the second = to a minus sign (-) so that the code reads as follows:

iReturn = iCurrYear - Age

6. In the Immediate window, run the code again (clicking OK on each message box, which should now hold the correct value for iReturn). The result should look like Figure 7-10.

Figure 7-10. The correct value is returned.
0 0

Post a comment