Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


1000


Learning about events in Access
Events are a bit more complex than properties or methods. Even though you constantly use events
in your applications, you never see an event (because events don’t exhibit a user interface), and
under most circumstances, you don’t deliberately invoke an event through your code. Events just
sort of happen when a user clicks on a command button or tabs off of a control. Events are just
there — you use them as needed.

A reasonable analogy for events is the ringer on your cellphone. Your phone rings whenever some-
one wants to talk to you. The ring alerts you to the incoming call, and you decide whether to
respond to the ring or ignore it.

From an object-oriented perspective, you add events to your objects so that the object has some
way of notifying its consumer that something has happened within the object or has happened to
the object. For example, consider a data-management object that reads and writes data from a data
source. The properties are easy to understand and may include the path to the data source, the
name of a table, and an ID value to use when extracting or saving data.

In this case, you may add an event to the data-management object that’s triggered when the data
source is unavailable, or when a record matching the ID value can’t be found. Using events is
much cleaner and more direct than relying on errors to be thrown when the data-management
object fails to complete its task.

Recognizing the need for events
To my knowledge, there is no limit on the number of events you can add to a class module. You
declare events in a class module’s header, and invoke the events within the class’s properties and
methods.

This process may make more sense if you consider a property procedure built in the “Adding a
new property to provide extra information” section, earlier in this chapter:

Public Property Get SupplierName() As String
Dim varTemp As Variant
If m_SupplierID <= 0 Then
Exit Property
End If
varTemp = DLookup(“CompanyName”, “Suppliers”, _
“SupplierID = “ & m_SupplierID)
If Not IsNull(varTemp) Then
SupplierName = CStr(varTemp)
End If
End Property

This property procedure returns the name of a product supplier, given the SupplierID (notice
that the SupplierID is obtained through the class-level m_SupplierID variable). The
SupplierName property assumes that the m_SupplierID property variable has already been
Free download pdf