Asynchronous Execution

Sometimes, Access runs two areas of your VBA code simultaneously, even though you've placed the code into different events or even in different forms and reports. This ability for Access to start running one procedure of code before another one is finished is called asynchronous execution. Most of the time asynchronous execution happens without you (or your user) really noticing, but it can sometimes cause problems, so you should know when it happens and what to do about it.

OpenForm

The most common asynchronous execution you'll encounter is when you open a form using the OpenForm command. Most of the time you won't notice it, but here's what really happens: When the OpenForm statement runs, the form you ask for starts to open, along with all of its Open, Load, and On Current events. However, your code after the OpenForm command also continues to run at the same time. Usually, not much happens at this point, so there's no harm done.

There are times, however, when you would like the execution of the code in the calling form to stop until the user is done with the form you open. This can happen when you are prompting the user for selection criteria during the Open event of a report (see Chapter 14), or when you open a form to add a new record from an index form.

In this latter case, you normally want to requery the index form to show the record that was just added, but you have to wait for the user to finish adding it. If you perform a requery right after the OpenForm, your code will continue merrily along and requery your first form, only within milliseconds after your second form has started to open. No matter how fast your user is, that's not enough time for them to add the new record. So your requery will run before the new record is added, and the new record will not appear on your index form.

There is a simple solution to the normal asynchronous execution of the OpenForm command. It's called Dialog Mode.

Dialog Mode to the Rescue

To prevent asynchronous execution when a form opens, use Dialog Mode. Instead of DoCmd.OpenForm FormName:="frmMyForm"Specify Dialog Mode instead: DoCmd.OpenForm FormName:="frmMyForm", windowmode:=acDialog Dialog Mode accomplishes two things:

□ It opens the form in Modal Mode, which prevents the user from clicking on any other Access windows until they are done with this form.

□ It stops the execution of the calling code until the newly opened form is either closed or hidden.

This second feature of Dialog Mode is what is so helpful in preventing Access from trying to run two areas of your code at once.

Notice that the code stops until the form is closed or hidden. This is the basis for many clever uses of Dialog Mode where values from the called form are used elsewhere. If you just hide the form (by setting its Visible property to False), the values on the form are still there and ready for you to reference, even though the code in the calling form now continues to run. This is the technique for gathering selection criteria and building SQL statements, which is described in Chapter 14.

0 0

Post a comment