Access VBA Macro Programming

(Joao Candeias) #1
Make sure you click the cursor on the body of the subroutine before you run it so VBA
knows which procedure to run. Your screen should now look like Figure 1-6. This figure
assumes you did not already have a database open, because the ribbon is grayed out in it.
See, you can produce professional-looking interfaces on Access with hardly any code!
This is a simple demonstration of adding code to a module. If you have used VBA in
Excel, you know there are events you can also add code to, such as Workbook_Open. Events
are also available in Access VBA on Forms and Reports. A similar example in Access would
be the Form Open event that is fired off every time that particular form is opened within the
database. This is discussed further in Chapter 9.
Events are being fired off all the time when things happen in an Access database. You can
insert code to take action on a particular event, such as a user making a selection on a form,
and each time the event happens, your code will be run.
However, you cannot do any editing in the code window until you click OK on your
message box and the macro finishes running. This is because the focus of the code is on
your message box window, and the focus cannot be moved anywhere else within Access
until the message box disappears.

10 Microsoft Access 2010 VBA Macro Programming


Figure 1-5 Code to display “Hello World” message box using MyCode
Free download pdf