jwcolby
jwcolby at colbyconsulting.com
Thu Feb 12 17:21:06 CST 2009
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