Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


1006


Here’s all the code behind this simple dialog box:

Public Event FormClosing(Message As String)
Private Sub cmdOK_Click()
DoCmd.Close acForm, Me.Name
End Sub
Private Sub cmdCancel_Click()
txtSomeData.Value = Null
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Close()
If Not IsNull(txtSomeData.Value) Then
RaiseEvent FormClosing(txtSomeData.Value)
Else
RaiseEvent FormClosing(“No data”)
End If
End Sub

A public event named FormClosing is declared at the top of the dialog form’s module. This
event returns a single argument named Message. The cmdOK_Click event procedure closes the
form, while the cmdCancel_Click event clears the contents of the text box named txtSome-
Data before closing the form.

The FormClosing event is raised by the dialog form’s Close event procedure, ensuring that the
event is raised whenever the form is closed. If the txtSomeData is not Null, the value of the text
box is passed by the FormClosing event, while a default message is passed if the text box’s value
is Null.

No other code is needed by the dialog form, and the form is allowed to close normally because the
FormClosing event fires just before the form disappears from the screen.

The main form is shown in Figure 28.16.

FIGURE 28.16

The main form sinks the custom event raised by the dialog form.


The code behind the main form is also quite simple. Notice the WithEvents keyword applied to
the form object’s declaration:
Free download pdf