51. VBA - Custom Events (Programming In Access 2013)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone and welcome back to programming in Access 2013 my name is Steve Bishop today we're going to be continuing our series on VBA or Visual Basic for applications today's topic is going to be events now events are special triggers that modules can look for okay so they're little triggers that are going to happen and modules can be looking for those triggers and with events you've got a publisher and a subscriber and these are two separate classes from each other you've got one class that's going to be the publisher and one class that is going to be the subscriber now the publisher creates the event and raises it so the publisher is going to go out and declare what an event is and it's also going to be the one that decides when to raise that event the subscriber is going to watch for the event to be raised and then perform an action when it's raised so this is the link between the two you've got one that is the publisher who's going to create and raise of it and trigger an event while the subscriber is going to be watching for when that event is triggered and then do something when that event is triggered so in your publisher the syntax is going to be looking like this where we have public event with an event name and then any parameters that you want to declare on your event so this is going to be cleared in your publisher and this public event event name with any parameters is the Declaration of that event okay creating an event you can think of it kind of like a property on your class okay you're declaring it here then you have somewhere in your code somewhere in your your publishers code you've got a method that you've already written out here and it's doing something and then when you want to trigger that event when you want to trigger this event that you named originally that you you know that you created you're going to use the raise event keyword along with the name of the event you want to raise and any arguments that are required to raise that event then of course you're going to end that subroutine then in the subscriber so that's this class that's listening for events in order to make it so that your subscriber is listening to the events is when you declare your object name of the class that holds the events in it you need to use the with events keyword and then later on in your code you're going to have this private sub object name whatever the name of the object was you declared up above with an underscore and then the name of the events that you want it to watch for okay so you're up above you're declaring your event with events okay you're going to use the with events keyword with the object name and then as whatever the class name was and the class name is the class that contains the event in it then you're going to do this private sub with the object name that you declared up above in your subscriber underscore and then the event name that you declared within the class that has the event then you're going to do something when that event is raised and then you're going to of course end your sub routine so you can see the relationship here between the two okay that it's really important that you understand one has an event that is just up that you're just posting it you're saying hello I'm go ahead and if anybody's listening go do whatever it is you want to do because now is the time and then on the other side and your subscriber you actually have something that is listening and going to do that do an action whenever that particular event is raised now there are certain rules for events that you need to be aware of events must be declared in the declaration section of your class which means you can't declare an event anywhere other than at the top of your of your class okay you can't declare it at the bottom you need to declare it all the way up at the top before any of your subroutines or functions methods any of that and I even go all the way up above any properties that I create too so it needs to be all the way up at the top of your class you cannot create circular publisher and subscribers which means you can't make it so that a publisher is both a publisher and a subscriber to the same class as your subscriber okay you can't have this back and forth between the two different objects it's a one direction thing okay and that's because access Microsoft doesn't want you to create an accidental infinite loop where you know one method one I'm sorry one event is cuz raised in your publisher class which then your subscribers listening to so it goes and does some action and when it's done doing that action it goes and raises its own event that the publisher then is now listening to and you know then it goes and does something again it doesn't Microsoft doesn't want you to be able to do it back and forth like that you only want you can only do it one directionally okay now there are certain ways to get around that you could actually create a third class if you want it to but that's a little bit more difficult than what we really need to get into and you'll find that it really is kind of unnecessary to do anything like that you really just want a publisher and subscriber one directionally now events must be declared with the public access modifier and if you think about it this kind of makes sense you want your event to be public so that anybody watching can can listen for that public event if it was a private event it really wouldn't make much sense because then no one would be able to see it the optional parameters are not allowed so the optional keyword in your parameters of your event are not allowed you cannot use optional for those parameters you if you declare a parameter within an event then any any time that you are raising that event you must pass in the required parameter named arguments are also not allowed to be passed and we haven't really talked about named arguments but there is a way basically to pass your values into a function or into an event where you actually name what is the parameter that you're trying to ascribe that particular value to so you don't have to necessarily do it in order of how they show up is the list of parameters you can kind of get your different values at different times well unfortunately with events that's not allowed you cannot name your arguments okay you have to go in the order in which they appear in the list of parameters and that's not really something we're gonna get all into I'm not really going to discuss named arguments anywhere on this course but you're welcome to go ahead and investigate those things we may use an instance where you use named arguments and that might be a good example to see how they're used but right like I said I'm not really going to cover them too much in this in this course now publishing object must the publishing object must be instantiated using the set keyword okay and up above in the line that's yellow you can kind of see how we've got this dim with events new object as new class name and that's how we've been declaring our objects lately except without the with events keyword we've just been dimming new object as new class name well you can't just use the with events inside of that declaration you'll notice below in the white I have dim with events new object as class name so we're dropping the new keyword when we're declaring our class name and then after we've dimmed our class we can then somewhere in our code use the set keyword in order to set the object into a new instance of the class okay and that's the what you have to do if you're going to use the with Vince keyword you cannot use the new keyword with your declaration of that object and then when you're going to instantiate that object you have to use a set keyword in order to use the new keyword to you know create that instance of your class name now events can only be created in class modules they cannot be created in regular modules and I know we haven't really gone over the distinction between class modules and regular modules yet but a class module is what we've been working on in the last couple of videos okay they're objects that have properties and methods in them and you can create also events in them but you can't create events on regular modules like the Global's module that we worked on some time back in earlier videos okay now regular modules can however be subscribers okay they can't you can't declare an event you can't create an event in regular module but you can listen for events in regular modules alright so those are some of the rules that you need to be aware of when you are working with events now let's go ahead and see an example of this let's go ahead and see how this all works out so I'm back to where it was before and there's my dimming of a new contact as new class contact which back over here is where we developed our class object before and remember we've got our init function or a knit method here that we need to run when we instantiate our object when we create our object here so what we've got to do and what I want to do is I want to make it so that when this init function both starts and ends it's going to declare it's going to pop up a message to the user now I could just simply go in here and I could say message box contact class in initializing there we go if I could spell correctly and then drop another message box down here that says contact class initialized okay so I could do that and we could we could certainly run it this way so that you know when the init method is run then we pop up a message box do our thing and then pop up another message box but we're doing all of that within this code let's say that we this is just kind of a theoretical example here let's say that we want to use we want this message box to pop up not from within this code but we actually want the caller whoever the object was here this form underscore form for we want it to be the one that responds and pops up the message box for whatever reason okay this is just an example you will we will be going more into in-depth reasons why you want to do events but this is just to kind of show you how the workflow works here so rather than set up my message inside of my init method here what I'm going to do is I'm going to go ahead and create an event on my class contact now my contact class is in fact where it is going to be the publisher okay it's the one that's going to send up a flag for anybody that's listening that an event is being triggered so we're going to use public key word for the as the access modifier public event oops and show message now I'm also going to add a parameter in here because I'm going to pass in some text to whoever is listening to this event I'm going to pass them some text and I'm going to call that variable that parameter a message and it's going to be of a type of string okay so we've got a public method called show message and we're going to pass in message as a string and again I don't need to pass in variables if I don't want to but in this particular case I'm going to create one and I will be passing information through it okay so now in my anit instead of doing my message box right here and there I'm going to do raise event show message and you can see when I put a space right after the raise event keyword it automatically intellisense knew that I was going to raise one of the events that I created already in the class object here and sure enough show message is the one I want so let's go ahead and tab in order to make that the one and then I need to make sure that in parentheses I am passing along that argue okay so here I'm raising the event show message and then the contact class initializing okay then down here I'm basically going to do the same thing I'm just going to go ahead and copy and paste this and down here I'm going to raise the same event with a different message okay so I'm going to trigger the the event show message at this point when the init method has been started to run when it's been initialized and I'm going to raise that event that shows message contact class initializing and then raise event show message class contact initialized now here's one of the key things that you need to understand about events at this particular point nothing is really happening okay I may be raising an event but unless there's somebody listening to event nothing is really going to happen okay I may raise the event pop in a message that that goes into here into my show message event but unless somebody is listening the application is really going to stop right there and what's great about that is that I don't necessarily I could put one of these events in here but I don't have to have anybody do anything because of it okay I can or I don't have to I could have someone subscribe to this event or I could just leave it the way it is and no one will be the wiser it's not that big of a deal okay so if I save it and I compile it you'll notice I don't get any sort of errors everything works completely perfectly fine okay so I created this event called show message and here it is up here I've got one parameter that I'm going to pass along to anybody who's listening so let's go back to our form here where we declared an object called new contact of a type of that class remember class contact is the publisher here okay so the form I want to make this form underscore form form the subscriber to that particular class okay to that particular event and the way that we need to do that is when we declared the object here as a type of that class this is where we would need to use the with events keyword but remember I declared it with this new keyword so if I try to go out of it here I'm going to get an error message it says invalid use of new keyword is again I can't use the new keyword if I'm also using the with events so I'm gonna go ahead and get rid of that new keyword there and now when I move off you'll see that it lets me go ahead and do that now that's all fine and good but I've got I mean now I've got this this object that is that access knows now I want to be watching out for any events that occur from this object called new contact so this new contact it may be raising some sort of event and I'm alerting access with this with events keyword that hey I'm looking for events that are going to be happening on this object okay and whenever something like that happens I want to do something now I have a couple of problems here number one this dim with events is declared inside of my form load which means no other part of my of my form underscore form form module here is able to see this new contact object okay because it declared it inside of the form load the only place that is going to be able to see that object that new contacts object is inside of the form load event so I can't see the new contact outside out here so if I wanted to create another private sub and I'm just going to call it whatever okay and then I wanted to use that new contact and I hit period notice I don't get any of the intellisense for that new contact object and that's because this this object only exists for the brief moment that we are running this forum load method that form load subroutine so once I hit this end sub then this new contact object goes away it disappears okay access goes in cleans up the memory and it's gone and I can't access it anymore so what we need to do is we need to move this I'm going to go ahead and delete this out of here first I need to move this new contact object out of the form load event and I'm going to put it up here in my declarations so now it is an object variable that is visible to all of the rest of my code now what I can do is well first I need to go ahead and set my object I need to instantiate it here so I'm going to go ahead and do that by using that set keyword new contact equals new CLS contact okay so now I'm IMing it as an object variable up here in my declaration section but here is where I'm actually going to instantiate it and that is on the form load event alright so there's my instantiating statement and this is very important to understand that you must use this syntax you know you got your declaration here and then you need to do you need to instantiate it with the new keyword somewhere else in your code all right now that I've got that done I could go in here and I could start manually typing private sub and if you if you recall I'm going to go back to the slide here you'll see that we had the private sub object name underscore event name so I could go through here and I could do this so the object name is new contact underscore and the event name was show message and then there was a variable called message as string okay and I could certainly do it that way that's fine but believe it or not there's a quick easy shortcut here once you get to this particular point and this is also a good way of validating to make sure that you've done everything right up to this point if I go up here to where we have these two dropdowns you may not have noticed these before we haven't really done anything with them but I can actually select objects and events on my on on my class right here so let's say I want to I want to go ahead and create that new sub routine you'll notice that new contact is an object that I have that is accessible so I can click it and it knows since I only have the one event that's in that class object it automatically just goes okay what that must be the event the that you want to be listening for and sure enough it creates our private sub new contact underscore show message message as string so it does that Association for us and this is a really good way to make sure you've done everything correctly up to this point because let's just say for example I'm going to take this out here just for a moment let's say I forgot to do the with events keyword here I'll watch this notice it's disappeared even though my object still exists up here in my declarations I don't have it from my drop-down list but when I use the width events keyword now it'll show up okay so then I select it and it automatically selects the show message event if I had multiple events on here it would I would still need to select which event I want to work with from this list and then from there I just go into here into my sub routine and now this is where I'm going to actually do something this is where I'm going to pop up that message box that displays the message so that's it that's all I've got to do I'm going to go ahead and set a breakpoint here and we'll follow along we'll follow the flow of how this all works how this publisher and subscriber really started really does its thing here so I'm going to go ahead and start my form oh come on you're being a pain aren't you there we go alright so there I've just loaded my form and the new contact object exists but there's nothing in it right now okay it hasn't been instantiated so I've got this object up there that's ready to go now we're going to go ahead and use that set keyword to go ahead and instantiate our object as a new class contact and now we're going to run that init method from our new contact and that jumps us over into the class contact this class module over here and we're going to run that an it method and now the first thing that happens the first thing that the method does is raise that event show message called with the message of contact class initializing so what's going to happen is it's just going to jump straight to anyone who is listening for that show message event and sure enough back here on my forum underscore form forum we've got that new contract underscore show message sub subroutine who is the subscribed method that's going to run when that event is raised so here we go message box and we're going to get contact class initializing because that was the message we passed in all right then when I go when I continue on through the code we'll jump from the form underscore forum' form back to the class contact contact because that was where we were running the init method and so now it's going to do its normal thing of setting the values setting the properties that we were working with before in this class and then it's going to raise the same event the show message event but it's going to pass in a slightly different a different value for the message text but it's going to jump again to this form underscore form form subroutine called new contact on the score show message and it's going to run the same method it's going to run the same subroutine ehn from this forum underscore form forum because we're raising the same event but this time the message is a little different contact class initialized so it's done and we're good then we're going to go ahead and skip on down to the next line and sure enough that is the end of the init method so we're at the end sub of that particular init method and that brings us back now to the end we're done now with that a knit method so we're going to jump back to on the forum underscore form form where the debug print is going to occur and that's going to go get a couple of values off of my class and post them down here to the immediate window so you can see how that works you can see how there's this connection now between this forum underscore form forum who's listening for events and listening for the specific event show message from the object new contact that was created inside of it here and then whenever something happened in this class that raised that event show message then whoever was listening to that event show message went ahead and did its subroutine down here it's it's action when that event was raised and that is how you have the connection between the two different class objects if you think about it we've actually already done something similar to this but we've just never gone over it and it was done automatically in the background by access notice that private sub forum load well this is the same exact thing as what we're doing here private sub new contact show message we have an object name with an underscore and then if event and that correlates to the events that we have over here on our forum there's the on load event so notice we have an event procedure you can even say it says event procedure right here and this was the code that we designed around the form load event okay so we've already worked with events but they've just automatically been created the syntax was already done for us by access when we clicked on those buttons now we're declaring our own particular custom events and then we're doing whatever we want with them whatever we want which is really really cool that's great about coding is you get to make the code do what you want when you want it to do it alright we're going to get a little bit more in depth into some of this some of the things you will be using these events for and I hope to see you in that next video
Info
Channel: Programming Made EZ
Views: 32,343
Rating: 4.9776535 out of 5
Keywords: Microsoft, Access, 2013, Programming, VBA, Visual Basic, Applications, tutorial, lesson, guide, database, SQL, beginner, advanced, software, microsoft access, table, query, form, code, coding, development, visual basic for applications, computer programming, events, custom events, subscriber, publisher
Id: kfIabBuUtmA
Channel Id: undefined
Length: 27min 11sec (1631 seconds)
Published: Sat Apr 05 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.