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 On Open, On 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 runs 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 this:
DoCmd.OpenForm FormName:="frmMyForm", windowmode:=acDialog
Note the use of named parameters in these examples — FormName: = "frmMyForm"/ for instance. Functions and subs in VBA can receive parameters (also often called arguments) using either positions or names. If the names are not specified/ VBA assigns parameters based on their position: first/ second/ and so on. When you see extra commas indicating missing parameters/ you know that positional parameters are being used. Named parameters are much clearer to read and understand/ and experienced programmers often use them.
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.
There is a disadvantage to using Dialog mode. While a form is open and visible in Dialog mode, any report that is opened will appear behind the form and won't be accessible. If you encounter this problem, you can use another technique to control the timing of form requeries. One technique is to open the second form normally and allow the code in the first form to complete. Then, put your requery code in the first form's On Activate event to fire when the focus returns to the first form.
Average user rating: 1 stars out of 1 votes
Post a comment