3.2 Write a simple subroutine

1.
PIC

Click “Create” in the ribbon, then click the drop down box and click “Module”.

2.
PIC

The previous step opens a Visual Basic IDE (integrated development environment).

3.
Next, type the following code in the main editor window.
Public Function autoexec()  
    MsgBox "Cool!", vbOKOnly, "Start up code"  
End Function  
    

PIC

Note that as you type, the Visual Basic IDE helps you along. For example, as soon as you press the ENTER key after typing the first line, the last line with End Function automatically appears. Also, as you type the second line, the VB IDE prompts possible values for the parameters.

4.
The previous step defines a subroutine in a new module. You can test run the subroutine right after typing it. Click the “Play” button to run the subroutine.

PIC

When this subroutine runs, it pops up a dialog box. As soon as you click “OK”, the dialog box disappears and the screen returns to the VB IDE.

5.
Now that we have tested the subroutine, it is time to save it and get back to the Access application. Click the floppy disk icon to save the module. The default name is “Module1”, you can rename it to a more appropriate name.
6.
There is no need to close the VB IDE. Instead, use the task bar to switch back to the Access application.