[AccessD] Demo sinking events in two places

McGillivray, Don [IT] Donald.A.McGillivray at sprint.com
Thu Feb 12 12:44:55 CST 2009


OK, I know I'm WAY more jazzed by this than I should be, but after following your instructions, the forms failed to behave as advertised.  So, pulling together what we've learned so far, I dug into the code, and uncovered a small but significant omission.  In the Form_Load event of the "...Sinking..." form, The reference to the "...Demo..." form's command button was established, but the OnClick event wasn't "hooked" by setting its value to [Event Procedure].  Inserted this line in the Form_Load event's code:

Cmd.OnClick = [Event Procedure]

and it worked.

I AM getting this . . .   :o)

-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Thursday, February 12, 2009 7:11 AM
To: Access Developers discussion and problem solving
Subject: [AccessD] Demo sinking events in two places

Boy did I ever mess that one up.  IF you did not overwrite your original form frmDemoCtls then you
SHOULD be able to:

* open frmDemoCtls FIRST
* Open frmDemoSinkingCommandButtonEvent
* Notice that the form pops up a message box telling you the name of that form plus the name of the
command button back on frmDemoCtls
* Click the button on frmDemoCtls
* Notice a message box telling you the name of frmDemoCtls plus the name of the command control
* Close that message box.  Notice a message box telling you the name of
frmDemoSinkingCommandButtonEvent plus the name of the control.

Whew.

So what happened?  frmDemoSinkingCommandButtonEvent has the following code inside of it:

Dim WithEvents cmd As CommandButton


Private Sub cmd_Click()
     MsgBox Me.Name & ": " & cmd.Name & ": Click event"
End Sub


Private Sub Form_Load()
     On Error Resume Next
     Set cmd = Forms!frmDemoCtls!Command15
     MsgBox cmd.Name
End Sub

Let's break that code down.

Dim WithEvents cmd As CommandButton

Here we dim a command button Withevents which tells VBA that this class will be sinking events for a
command button named cmd.

Private Sub Form_Load()
     On Error Resume Next
     Set cmd = Forms!frmDemoCtls!Command15
     MsgBox cmd.Name
End Sub

The load event reaches over to frmDemoCtls and grabs a pointer to Command15, and SETs cmd = to that
pointer.  It then pops up a message box telling you the name of the command button.  I placed the
OnError line in there in case frmDemoCtls was not open yet.  The code will NOT run correctly if you
load frmDemoCtls last.

Private Sub cmd_Click()
     MsgBox Me.Name & ": " & cmd.Name & ": Click event"
End Sub

And finally, we build an event sink in frmDemoSinkingCommandButtonEvent to run when the command
button is clicked.

This might be a strange concept, sinking an event from an object in a form somewhere else but that
is precisely what our clsCtlXXX is doing with combos, text boxes and eventually other control
wrappers for objects such as radio buttons etc.  The important thing to understand in all this is
that an event can be sunk in as many places as you want.  All you have to do is set it up.
frmDemoSinkingCommandButtonEvent simly demonstrates how to sut up such a scenario.

In this lecture we have demonstrated that a form can sink an event for a control on another form.
We have also demonstrated again that the form that contains the control gets code execution control
first and runs its code.  Once it is finished, any other class that is also sinking that event gets
control, in this case frmDemoSinkingCommandButtonEvent gets control.

Isn't this stuff FUN?

;-)

--
John W. Colby
www.ColbyConsulting.com
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


This e-mail may contain Sprint Nextel Company proprietary information intended for the sole use of the recipient(s). Any use by others is prohibited. If you are not the intended recipient, please contact the sender and delete all copies of the message.





More information about the AccessD mailing list