16 min read
Writing a Simple VBA Procedure
A Sub performs actions; a Function calculates and returns a value you can use inside a query or a form's control, just like a built-in function.
Field Notes: Two Kinds of Procedure
A procedure is a named block of VBA code that performs a task. The two most common kinds are a Sub, which performs actions but doesn't return a value, and a Function, which calculates and returns a value, usable inside queries and controls, just like a built-in function.
A simple example, showing a message box:
A custom function follows the same shape, but returns a value: define it with Function and End Function, and once saved in a standard Module — Insert, then Module, from the Visual Basic Editor — it can be used inside a query's calculated field or a form's control, exactly like a built-in function.
More lessons in Microsoft Access · Next: Responding to Events with VBA · Previous: The Visual Basic Editor
