Access VBA Macro Programming

(Joao Candeias) #1

You may want to use the After Insert event to create a child record in another table that is
related to the new record being created.


After/Before Update


After Update and Before Update events are fired off before and after the update of a record.
No warning message is provided, such as in the case of a deletion, so you may wish to
provide an “Are you sure message” on the Before Update event:


Private Sub Form_BeforeUpdate(Cancel As Integer)
x = MsgBox("Are you sure that you wish to amend this record?")
If x = vbNo Then Cancel = True
End Sub


You may want to use the After Update event to amend child records in another table
related to this parent record.


Click


A Click event is fired off when the user clicks the mouse somewhere on the form. Do not
forget that controls also have their own on-click events so you need to guard against where
the user is clicking.


Close


The Close event happens when the form is closed, either by the user clicking the Close
button, or through your own code. You may use this event to open another form by using:


Private Sub Form_Close()
DoCmd.OpenForm ("MyForm")
End Sub


Current


The On Current event occurs when the user moves to a record, which then becomes the
current record, or when the form is re-queried or refreshed. From its name, it is easy to
confuse it with the Load event.


Load


The Load event occurs when the form is opened and its records are displayed.


Open


The Open event differs from the Load event in that the Open event does not occur until the
form query has been run. At this point, your form has data on it and your code can interact
with it, whereas when a load event occurs, the controls have not been populated.


Chapter 9: Forms and Reports 113

Free download pdf