Microsoft Access VBA Macro Programming

(Tina Sui) #1

The name of the function isMultiply, and this is used as a variable to return the answer.
This is the only way to return the answer to the routine that called the function. Note that the
name of the function now appears in the drop-down at the top right of the code window. This
is because it is now an official function within both your VBA code and Access.
You can now use this function in two ways: by calling it directly from within a SQL query
as a function, or by using it within your VBA code. To call it directly from within a SQL
query, use “select Multiply(3.4) from MyTable;”. Having your own custom functions opens
up interesting possibilities as to what you can now build into your queries.
However, a word of warning when running custom function queries over large amounts of
data. Close down the VBE window before you run the query, otherwise this window will
keep being updated as the query is run, meaning the query will take considerably longer to
run than normal.
Now, for the second way to use the function: calling it from within your VBA code. For
the sake of simplicity, you will next call your new function using the same event from which
you called the Hello World example.


Chapter 3: Modules, Functions, and Subroutines 31


Figure 3-1 Creating a simple multiply function

Free download pdf