DWUTKA at marlow.com
DWUTKA at marlow.com
Tue Jun 27 14:21:38 CDT 2006
Our custom collection contains all of the people in our database, represented individually by the Person class. However, our Person class can do things on it's own, that our People class should be aware of. For example, you can change, add or delete a user with just the Person class, yet the People is not aware of that. So let's fix that. First of all, the purpose of doing this is so that everything using the People class is aware of changes to the Person(s) it contains. We are going to create a custom event for our People class. An event is the other part of the 'definition' of VB/VBA. VB/VBA is an event driven Object Oriented Programming language. Event Driven. Everything that occurs in our projects is driven by events. A user clicks a button (OnClick event), a user opens a form (OnLoad/OnOpen). Pre-existing classes (forms, reports, controls) have events, it's time we utilize the same functionality. To keep our project 'synchronized' we need to create an event in our People class to let everyone know that something has changed with the Person(s) it contains. Let's define our event. In the declarations section of our People class (at the top), let's put: Event PeopleUpdated() Now, where do we 'raise' this event. Raising event means that the running code fires off the event, and any class that has 'sunk' that event will be triggered. We want to raise it where it makes sense to do so. In our People class, we build our internal collections with the GetPeople sub. Let's raise our event at the end of that. So before the End Sub line, put: RaiseEvent PeoplUpdated (Note that PeopleUpdated came up with Intellisense after we typed RaiseEvent) Now we need to have the Person class let the People class know when one of them has changed. There are a few ways to do this. There have been many discussions, some heated, on the list about Global Variables. We are going to use a global variable, but if you would like more information on how to do this in other methods, feel free to search through the list archives. Again, this is a demo, and I am just trying to demonstrate functionality. I personally use Global Variables in cases like this, but other developers use other methods for other reasons. So create a new module, and call it modGlobalVariables. In the declarations put: Dim demoPeopleClass As People Save the module. To make the Person class interact with our global People class, we could either reference the global each time, or we could create a local reference to the global class. We are going to use the local reference. We are going to do so, because right now the only event we have doesn't affect the individual Person(s), we may add other events, so we might as declare a reference withevents. So in our Person class, we are going to add the following line in the Declarations: Dim WithEvents PeopleClass As People When do we need to alert the global People class? And how do we do that? Well, we need to alert it when either the Save or Delete procedures have run. To alert it, let's create a function in our People Class: Friend Function APersonHasBeenChanged() GetPeople End Function Now, in our Person class, at the end of the Delete and Save procedures, let's add: PeopleClass.APersonHasBeenChanged With the changes we have made, we now need to modify our frmPeople form, so that it deals with the global People class, and is aware of what is happening. We'll do this in the next part. Drew