Microsoft Access 2010 Bible

(Rick Simeone) #1

Part II: Programming Microsoft Access


470


FIGURE 12.9

Running an event procedure when a form closes


The Form_Close event illustrated in Figure 12.9 first checks to see if the form frmDialogPro-
ductPrint is open. If it is open, the statement to close it executes. Although trying to close a
form that isn’t currently open doesn’t cause an error, it’s a good idea to check to see if an object is
available before performing an operation on the object.

Using an event procedure to confirm record deletion
Although you can use the Access Form View ribbon to delete a record in a form, a better practice is
to provide a Delete button on the form. A Delete button is more user-friendly because it provides a
visual cue to the user as to how to delete a record. Plus, a command button affords more control
over the delete process because you can include code to verify the deletion before it’s actually pro-
cessed. Or you might need to perform a referential integrity check to ensure that deleting the
record doesn’t cause a connection to the record from some other table in the database to be lost.

Use the MsgBox() function to confirm a deletion. cmdDelete’s event procedure uses MsgBox()
to confirm the deletion, as shown in Figure 12.10.

When the cmdDelete_Click() event procedure executes, Access displays a message box
prompt, as shown in Figure 12.11. Notice that the message box includes two command buttons:
Yes and No. Access displays the prompt and waits for the user to make a selection. The record is
deleted only when the user confirms the deletion by clicking the Yes button.

Caution
Before the RunCommand acCmdDeleteRecord statement executes, it automatically checks to see if deleting
the record violates referential integrity rules that you’ve set up in the Relationships diagram. If a violation
occurs, an Access error message displays and the deletion is canceled.


Cross-Reference
See Chapter 2 for more information on setting up referential integrity in a database.


Cross-Reference
The MsgBox function is discussed in detail in Chapter 26.

Free download pdf