jwcolby
jwcolby at colbyconsulting.com
Thu Jan 22 08:26:45 CST 2009
There is some discussion going as to how to communicate between forms. Using Withevents in classes (c'mon guys, it is EASY!!!) you can send messages between forms, passing parameters, or even causing code to run. To see a demo of this, go to my web site, log in then click example code / withevents demos on the menu. Unzip and open the demo database. www.colbyconsulting.com. The code is VERY simple! You can think of the message class as email, where the sender transmits a message with a From, To, Subject and Message. This allows the sender to identify itself, as well as the target of the message, what the message is about and a generic "message". Whether all that is necessary is up to your code, but allows you to use it if you wish. The receiving code can then set filters to only accept messages sent to itself, and can then use the subject to (for example) place text in a text box, execute a function, run a query, etc. Because the clsMessage is a class, you can create one or a hundred message channels, making private channels or public channels. Because the message class uses variants, you can even pass in pointers, which means that your subject or msg fields can contain pointers to controls, forms, recordsets etc. IOW the calling code can open a form (for example) and tell the form "return a value in this control". Or it could open a form and tell the form "move to this PK". The OpenArgs method of communicating between forms is limited to the opening form, and is rather clumsy as well. Having an "email" system where code can send email to other code opens up interform communication and makes it dead simple. Create a class called clsMsg and insert the following code (watch for line wraps): 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) End Function Function SendSimple(varMsg As Variant) RaiseEvent MessageSimple(varMsg) End Function '************************* Create a module called basInitMsg and insert the following code: Option Compare Database Option Explicit Public gclsMsg As clsMsg Function InitClsMsg() Static blnInitialized As Boolean If blnInitialized = False Then Set gclsMsg = New clsMsg blnInitialized = True End If End Function '************************** Insert the following code in the header of any form to use the message channel: Option Compare Database Option Explicit Private WithEvents fclsMsg As clsMsg Private Sub Form_Open(Cancel As Integer) InitClsMsg 'Initializes the channel Set fclsMsg = gclsMsg 'Gets a pointer to the class End Sub '************************** To receive a message from any code using the message channel: Private Sub fclsMsg_Message(varFrom As Variant, varTo As Variant, _ varSubj As Variant, varMsg As Variant) If varTo = "frm1" Then txtReceive = varMsg End If End Sub '*************************** To send a message on the message channel: Private Sub txtSend_AfterUpdate() fclsMsg.Send "frm1", "frm2", "Just a test", txtSend.Value End Sub -- John W. Colby www.ColbyConsulting.com