You are on page 1of 1

6 MICROSOFT ACCESS 2019 PROGRAMMING BY E XAMPLE WITH VBA, XML, AND ASP

to an item of data. Because we want the function to perform a calculation, we


specify that the variables will hold integer values. Variables and data types are
covered in detail in Chapter 3, “Access VBA Fundamentals.”
The variable definitions (the lines with the Dim statements) are followed by
the variable assignment statements in which we assign specific numbers to
the variables num1 and num2. Finally, the calculation is performed by adding
together the values held in both variables: num1 + num2. To return the result
of our calculation, we set the function name to the value or the expression we
want to return:
addTwoNumbers = num1 + num2

Although this function example returns a value, not all functions have to re-
turn values. Functions, like subroutines, can perform actions without return-
ing any values.
Like procedures, functions can accept arguments. For example, to make our
addTwoNumbers function more versatile, we can rewrite it as follows:
Function addTwoNumbers2(num1 As Integer, num2 As Integer)
addTwoNumbers2 = num1 + num2
End Function

Now we can pass any two numbers to the preceding function to add them to-
gether. For example, we can write the following statement to display the result
of the function in a message box:
Sub DisplayResult()
MsgBox("Total=" & addTwoNumbers2(34,80))
End Sub

3. Event procedures
Event procedures are automatically executed in response to an event initiated
by the user or program code or triggered by the system. Events, event proper-
ties, and event procedures are introduced later in this chapter. They are also
covered in Chapter 9, “Getting to Know Built-In Tools for Testing and Debug-
ging.”
4. Property procedures
Property procedures are used to get or set the values of custom properties for
forms, reports, and class modules. The three types of property procedures
(Property Get, Property Let, and Property Set) begin with the Property key-
word followed by the property type (Get, Let, or Set), the property name, and a
pair of empty parentheses, and end with the End Property keywords. Here’s an
example of a property procedure that retrieves the value of an author’s royalty:

You might also like