Part II: Programming Microsoft Access
442
- Move to the bottom of the module, and enter the following code:
Public Function CalcExtendedPrice( _
Quantity As Integer, _
Price As Currency, _
Discount As Double _
) As Currency
Dim ExtendedPrice As Currency
ExtendedPrice = Quantity * Price
CalcExtendedPrice = ExtendedPrice - (ExtendedPrice * Discount)
End Function
The first statement declares the variable ExtendedPrice as the currency data type.
ExtendedPrice is used in an intermediate step in the function. The next line of code performs a
calculation assigning the product of two variables, Quantity and Price, to the ExtendedPrice
variable. You might notice that the Quantity and Price variables are not declared within the func-
tion; these variables are explained in the next section, “Handling parameters.”
Finally, the last line of code performs one more calculation to apply any discount to
ExtendedPrice. The function’s name is treated as if it were a variable and is assigned the value
of the calculation. This is how a function gets the value that it returns to the calling program.
Handling parameters
Now, the question you should be asking is: Where did the Quantity, Price, and Discount
variables come from? The answer is simple. They’re the parameters passed from another proce-
dure, as you may have already guessed.
Parameters (often called arguments) passed to a procedure are treated like any other variable by the
procedure. Parameters have a name and a data type and are used as a way to send information to a
procedure. Parameters are often used to get information back from a procedure, as well.
The following table shows the names and data types of the arguments used in the
CalcExtendedPrice function.
Parameter Name Data Type
Quantity Integer
Price Currency
DiscountPercent Double
These parameter names can be anything you want them to be. Think of them as variables you would
normally declare. All that’s missing is the Dim statement. They don’t have to be the same name as the
variables used in the call to the function. Very often, you’ll pass the names of fields in a table or con-
trols on a form or variables created in the calling procedure as parameters to a procedure.