Chapter 14: WithEvents and RaiseEvent

team lib

Overview

Events are the life-blood of Windows and of Access forms and controls. Responding to events is an integral part of Access development. You have already seen dozens of examples where we ran our own code on a button click event or a combo after update. In a sense, writing our own event handlers to make our forms or controls do useful things is one of the key differences between a developer and a power user . Access has many wizards that can build up command buttons and similar controls for us, but to be able to do that ourselves , to really understand event handlers, is the key to really controlling our applications.

WithEvents is one of the best-kept secrets in Access. Do a keyword search of WithEvents in any of the Access or VBA help files and you get nothing! If you find anything at all, it will be simply that it is a keyword - no explanation of what it does, where or why you would use it. Start looking in all of the books in the bookstores. Most of them don't even mention the term anywhere . So what are they and why would we use them? Once you learn the how and why, you soon realize that handling events in classes is a tremendously powerful encapsulation tool, making it difficult to understand why they have been kept so quiet.

In Chapter 12 we learned how to build our own classes to encapsulate behaviors that would be useful to implement in multiple places in our databases. What if we could somehow marry classes and object events into a tightly integrated, completely encapsulated system that performed some useful behavior for us? Imagine being able to build a class that could cause a control to do exactly the same thing on any form we wanted. For example, we could build a class that allows us to handle the Enter and Exit events for textboxes. As the textbox gets the focus we could do something like change the font or font color or background color . That is the exact purpose of WithEvents - handling an object's events inside of classes other than the form classes.

WithEvents requires us to think about events a little differently than we have before. Events have more power than we commonly believe and can be used in ways not understood by the average Access developer. In order to learn these things we need to do a little reviewing of what we currently understand, learn some new terms, and stretch our imaginations just a bit. The next couple of pages will require a little more concentration, but once you learn the terms and concepts, the actual implementation is simple. When we say simple, we mean that we only need to use about eight lines of code in a class module and five lines in the form's module, as shown below, to start using WithEvents .

Class code:

   Private WithEvents mcbo As ComboBox     Public Function init(lcbo As ComboBox)     Set mcbo = lcbo     mcbo.AfterUpdate = "[Event Procedure]"     End Function     Private Sub mcbo_AfterUpdate()     MsgBox "this is the combo After Update event"     End Sub   

Form code:

   Dim fclsCboSimple As clsCboSimple     Private Sub Form_Open(Cancel As Integer)     Set fclsCboSimple = New clsCboSimple     fclsCboSimple.init Combo0     End Sub   

As you can see from the above code, the implementation is really simple, so bear with me. WithEvents provides the developer with considerable capabilities, and learning how to use this simple concept will literally move you to a new level of development ability.

In this chapter we will be looking at the following:

  • Reviewing events

  • What WithEvents is

  • Why we use WithEvents

  • How to build a simple class that uses WithEvents

  • How to build a Record Selector class that demonstrates reusability

  • How to have our class raise an event of its own!

 
team lib


Beginning Access 2002 VBA
Beginning Access 2002 VBA (Programmer to Programmer)
ISBN: 0764544020
EAN: 2147483647
Year: 2003
Pages: 256

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net