Chapter Summary

In this chapter you worked with a custom Microsoft Access application and learned how to write event procedures for various controls placed on an Access form. There are a number of other event procedures not discussed here that control how the Asset Management form and its controls respond to the user's actions. As you explore this application on your own you will start noticing the areas where writing additional event handlers would prove beneficial to the application's users. So get to it. Tear this sample application apart. Rebuild it. Add new features. Change the user interface if you want. Learn how to handle whatever event may come your way. And be prepared, because events happen frequently in an Access application, and sooner or later you'll need to respond to them.

In the next chapter you learn how to handle an object's events from classes other than the form classes, as well as how to program and raise your own events.

More about Event Programming

So far in this book you've worked with event procedures that executed from the form or report class module when a certain event occurred for a form, report, or a control. You probably noticed that event programming, as you've seen it implemented in the form and report class modules, require that you copy and paste your existing event code into new form or report events in order to obtain exactly the same functionality. For instance, say you added certain features to a text box on one form and now you'd like to have a text box on other forms behave in the same way. You could react to the text box's events in the same way on all your forms by entering the same event procedure code in a form class module for each form, or you could save keystrokes by learning how to centralize and reuse your event code.

You can avoid typing the same event procedure code again and again by using classes. Recall that we've already used classes in this book — in Chapter 8 you learned how you can design your own objects in VBA by writing code in a standalone class module. You worked with property procedures that allowed data to be read or written to the object. You also learned how to create functions in a class module that worked as object methods. In this chapter you learn how to react to an object's events from a standalone class module. The following new VBA terms will be introduced:

Event Sink — A class that implements an event. Only classes can sink events.

Event Source — An object that raises events. An event source can have multiple event sinks. Note: Source and sink terminology is derived from electronics. A device that outputs current when active is said to be sourcing current. A device that draws current into it when active is said to be sinking current.

WithEvents — A keyword that allows you to handle an object's events inside classes other than form or report classes. The variable that you declare for the WithEvents keyword is used to handle an object's events. Event — A statement used to declare a user-defined event. The Event declaration must appear in a class module.

RaiseEvent — A statement used to call a custom event. The custom event must first be declared using the Event statement.

Event Programming in Forms and Reports

0 0

Post a comment