jwcolby
jwcolby at colbyconsulting.com
Tue Feb 17 14:33:37 CST 2009
ROTFL. Ooops. The OhNoSecond strikes again.
THE MESSAGE CLASS
The following class demonstrates raising events. It is about as simple a class as you will find,.
It has a pair of events that it can raise, and a pair of methods that can be called and passed in
variables. The methods simply raise the event and pass along the variables passed in.
• In the demo database, click Insert / Class.
• Cut and paste the following code into that class
Option Compare Database
Option Explicit
Public Event Message(varFrom As Variant, varTo As Variant, _
varSubj As Variant, varMsg As Variant)
Public Event MessageSimple(varMsg As Variant)
Function Send(varFrom As Variant, varTo As Variant, _
varSubj As Variant, varMsg As Variant)
RaiseEvent Message(varFrom, varTo, varSubj, varMsg)
' Debug.Print "From: " & varFrom & vbCrLf & "To: " & varTo & vbCrLf & "Subj: " & varSubj & vbCrLf
& "Msg: " & varMsg
End Function
Function SendSimple(varMsg As Variant)
RaiseEvent MessageSimple(varMsg)
' Debug.Print varMsg
End Function
• Compile and save the class as clsMsg
Public Event Message(varFrom As Variant, varTo As Variant, _
varSubj As Variant, varMsg As Variant)
Public Event MessageSimple(varMsg As Variant)
Here we have defined TWO events that this class can raise. Notice that we are defining several
parameters that the Event will pass along to the event sink.
Function Send(varFrom As Variant, varTo As Variant, _
varSubj As Variant, varMsg As Variant)
RaiseEvent Message(varFrom, varTo, varSubj, varMsg)
' Debug.Print "From: " & varFrom & vbCrLf & "To: " & varTo & vbCrLf & "Subj: " & varSubj & vbCrLf
& "Msg: " & varMsg
End Function
This is the first event and mimics an email with a From, To, Subject, and Body.
Function SendSimple(varMsg As Variant)
RaiseEvent MessageSimple(varMsg)
' Debug.Print varMsg
End Function
This code is a very simple send routine that just passes along a variable.
• In this case we need to build a module to initialize and tear down this message class. Click
Insert / Module.
• Cut and paste the following code into that module.
Private mclsMsg As clsMsg
Function mMsgInit()
If mclsMsg Is Nothing Then
Set mclsMsg = New clsMsg
End If
End Function
Function mMsgTerm()
Set mclsMsg = Nothing
End Function
Function cMsg() As clsMsg
mMsgInit
Set cMsg = mclsMsg
End Function
• Compile and save as basInitMsg.
Private mclsMsg As clsMsg
Notice that we have a PRIVATE global variable mclsMsg.
Function mMsgInit()
If mclsMsg Is Nothing Then
Set mclsMsg = New clsMsg
End If
End Function
Here we have a function that initializes the mclsMsg variable ONLY IF the pointer is not already
initialized.
Function mMsgTerm()
Set mclsMsg = Nothing
End Function
Here we have a function that will clean up the class whenever we no longer need it.
Function cMsg() As clsMsg
mMsgInit
Set cMsg = mclsMsg
End Function
This function gets the pointer to the class, initializing it if it isn’t already initialized.
This email defines a new clsMsg. This class can raise two events, a msg and a msgSimple. Each can
pass on parameters. We also define a module where we can set up, tear down, and get a pointer to
the message class. I will create a demo for this class later tonight.
John W. Colby
www.ColbyConsulting.com
Jack and Pat wrote:
> Could some one please forward to me the email related to JC's # 19?
> I don't have it although I have the others.
> I was having considerable difficulty with CLSMSG DEMO
> and USING CLSMSGDEMO, especially cMsg() and
> Private WithEvents mclsMsg As clsMsg
> , but now realize that I was really missing the # 19 email.
>
>
> Thanks,
>
> jack
>
>
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
> Sent: Friday, February 13, 2009 11:50 AM
> To: Access Developers discussion and problem solving
> Subject: [AccessD] Class lecture emails
>
> I show the following emails as lectures:
>
> 1) [AccessD] Classes and Events
> 2) [AccessD] CREATE CLASSES AND EVENTS DEMO DATABASE
> 3) [AccessD] LOAD THE FORM CLASS IN THE FORM
> 4) [AccessD] Get event sinks working
> 5) [AccessD] Building a Control Scanner
> 6) [AccessD] Building a control class
> 7) [AccessD] MODIFY THE FORM CLASS TO LOAD THE NEW COMBO CLASS.
> 8) [AccessD] Classes and Events - EVENTS NOT REQUIRED
> 9) [AccessD] BUILDING A TEXT CONTROL CLASS
> 10) [AccessD] The classes as they are at this instant - clsTimer
> 11) [AccessD] The classes as they are at this instant - clsCtlCbo
> 12) [AccessD] The classes as they are at this instant - clsCtlTxt
> 13) [AccessD] The classes as they are at this instant - clsFrm
> 14) [AccessD] The form
> 15) [AccessD] IMPORT THE FORM
> 16) [AccessD] SINKING EVENTS IN MULTIPLE PLACES
> 17) [AccessD] EMERGENCY HALT: was Re: The form for demo event sink in two
> places
> 18) [AccessD] Demo sinking events in two places
> 19) [AccessD] ClsMsg
> 20) [AccessD] CLSMSG DEMO
> 21) [AccessD] USING CLSMSGDEMO
>
> Not too bad for 5 days work.
>
> If you work through all of these lectures you will be awarded membership to
> the AccessD Order of the
> Classy Developers. More importantly, you will have done what too few Access
> programmers ever do,
> started down the road to mastering classes and events.
>
> Do or do not, there is no try.
>
> John W. Colby
> www.ColbyConsulting.com
>
>
> Johncliviger at aol.com wrote:
>> Hi JWC
>>
>> I started following the lectures on Tuesday so I'm behind. However I'm
>> working my way thru it all. But I'm not sure whether I've got all the
> essential
>> bits. How many lectures have you done to date?
>>
>> Your modus operandi is great and keeping going
>>
>> Regards
>> john cliviger
>>