Create a blank database in Access. Click “Database Tools” on the ribbon, then click “Visual Basic”. This should bring up the Visual Basic environment.
In the “Project” pane (upper left corner), scroll down to “Modules”. Right click “Modules”, then select “Insert”, then “Module”. This will add a new module named “ModuleN” (N is a number). You can use the “Properties” pane to change the name of the module.
The editor portion should automatically switch to the code of this newly created module. The code should be blank to begin with. Append the following code to the editor:
Yes, the function calls itself! This is called recursion in programming terms. Don’t worry about this, the function works and it computes the nth Fibonacci number in the series.
Now, switch back to Access. Click “Create” on the ribbon, and select “Query Design”. You won’t be referring to any tables in this simple example (click “Cancel” when asked which table to include). Type the following in the “Field” row of the first column of the query:
First, select “SQL View”, then select “Datasheet View” to run the query. A result of 55 should display.
Of course, the chances that you need to compute a Fibonacci number in a database query is slim. However, this example does illustrate the fact that you can define your own function and use it in a query in Access.