Hi Folks, I'm working through an old manual that provides an introduction to VBA programming in Access and I'm stumped at their use of the IsMissing funtion. The code they provide is thus:-
Function MakeIceCream (Optional CocoaToBeAdded As Variant) As String
If IsMissing(CocoaToBeAdded) Then
CocoaToBeAdded = True
Else
CocoaToBeAdded = False
End If
rest of code.....
End Function
Depending on the value of the CocoaToBeAdded parameter (True or False) that is passed into the MakeIceCream function, the output will either state that you have made Chocolate or Plain ice cream and when testing this I get the results that the book promised . I understand that the IsMissing function tests for whether the parameter has been supplied and if not it sets CocoaToBeAdded = True, and true to it's word when the code runs without a parameter being passed in, I end up with Chocolate Ice Cream. What I don't understand is why the code behaves the way it does when a parameter value of True is passed in. From how I read the code the IsMissing function tests to see whether a parameter has been supplied, finds that it has, the If IsMissing part is therefore False and so proceeds to the Else part of the statement, where it then sets CocoaToBeAdded = False. I would expect that the end result would be Plain Ice Cream. However, that's not the result that I get when I run the code and I don't understand why. It makes sense if I take it that the If..Then..Else statement is just handling the test of the IsMissing function in that if the parameter is missing then the Ismissing value =True Else the parameter is present therefore Ismissing =False, but from how I understand it the code is saying that if the IsMissing function returns a Value of False(so a value has been passed) in, then set CocoaToBeAdded to False, irrespective of whether you passed in a value of True(or any other value). It's tooooo early! Can somebody explain this please. Cheers, John
Function MakeIceCream (Optional CocoaToBeAdded As Variant) As String
If IsMissing(CocoaToBeAdded) Then
CocoaToBeAdded = True
Else
CocoaToBeAdded = False
End If
rest of code.....
End Function
Depending on the value of the CocoaToBeAdded parameter (True or False) that is passed into the MakeIceCream function, the output will either state that you have made Chocolate or Plain ice cream and when testing this I get the results that the book promised . I understand that the IsMissing function tests for whether the parameter has been supplied and if not it sets CocoaToBeAdded = True, and true to it's word when the code runs without a parameter being passed in, I end up with Chocolate Ice Cream. What I don't understand is why the code behaves the way it does when a parameter value of True is passed in. From how I read the code the IsMissing function tests to see whether a parameter has been supplied, finds that it has, the If IsMissing part is therefore False and so proceeds to the Else part of the statement, where it then sets CocoaToBeAdded = False. I would expect that the end result would be Plain Ice Cream. However, that's not the result that I get when I run the code and I don't understand why. It makes sense if I take it that the If..Then..Else statement is just handling the test of the IsMissing function in that if the parameter is missing then the Ismissing value =True Else the parameter is present therefore Ismissing =False, but from how I understand it the code is saying that if the IsMissing function returns a Value of False(so a value has been passed) in, then set CocoaToBeAdded to False, irrespective of whether you passed in a value of True(or any other value). It's tooooo early! Can somebody explain this please. Cheers, John