[AccessD] Implements vs WithEvents

Hale, Jim Jim.Hale at FleetPride.com
Thu Jan 15 14:48:20 CST 2004


Very nice explanation, thanks.
Jim Hale

-----Original Message-----
From: John W. Colby [mailto:jwcolby at colbyconsulting.com]
Sent: Tuesday, January 13, 2004 9:03 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] Implements vs WithEvents


Robert,

Risking sounding ignorant, I don't know what the VBA DHB is so I can't help
you there.

However AFAIK you can't use Implements for something like a control or form.
These objects have code inside them that cause things to happen, properties
to modify their behavior etc.  ALL THAT STUFF can actually be inherited in
VB.NET.  That is, you can actually start with a combo box, subclass that
combo box, GET ALL OF IT'S FUNCTIONALITY, all of it's code, all of its
properties and methods, plus add your own!!!  Or even override things to
change the way the combo box functions.  THAT is true inheritance.

Not so in Access / VBA.  An object like a combo box is a self contained unit
that you simply cannot get inside of or modify in any meaningful way.  It
appears to us Access programmers almost like an ocx, i.e. a black box that
does things.

IMPORTANT... one of the things these objects do is raise Events!  IOW, they
tell you when a user clicks the mouse, double clicks the mouse etc. "inside"
their boundaries on the screen.

Knowing this, every Access programmer ALREADY USES WITHEVENTS.  How?  By
creating an event stub in a form's class, and inserting code in that event
stub to do something that the programmer wants to do when that event
happens.  The confusing thing is that a form's class DOES NOT REQUIRE a
withevents dimension statement for any controls contained in the form (or
for itself - the form - for that matter).  However there is an IMPLICIT dim
statement in the class, invisible, but there none the less.

So every Access programmer already knows all about handling events for
combos, command buttons etc.  They (most of us anyway) use the built-in
wizards in the form's builder interface to insert event handler stubs into
the form's class and insert code there.  THAT IS WITHEVENT PROGRAMMING, with
all the details taken care of for you.

Well guess what, ANY CLASS can have those same event handlers in them!!!
The only differenc is that if the class is not a form class, there must be
an EXPLICIT dim Withevents statement for any control (any object actually)
that you wish to sink events for.

'inside my own combo class header...
'Dim a combo withevents
Private WithEvents mcbo As ComboBox

Having the dim statement does nothing more than tell my combo class that a
control of a specific type MAY be transferring control of one or more of
it's events to the class directly.  It does NOT say WHICH control.  To do
that you have to pass in a control to the class and inside the class, SET
the dimmed control = the passed in control.

'inside my own combo class...
'Pass in a control and SET the internal control variable
function Init(lcbo as ComboBox)
	set mcbo = lcbo
end init

THAT tells the class which combo to sink events for (the one being passed in
of course).

Once you do that, the class can sink ANY of the control's events.  But you
still haven't told the class WHICH events to sink.    IOW, Even having done
this, nothing will happen in your class unless you create an event stub for
the control event you wish to process inside the class.

'inside my own combo class...
'create an event stub to which control will be passed when a specific event
fires.
Private Sub mcbo_AfterUpdate()
	'Do Something really cool
	Msgbox "Something really cool"
End Sub

THAT tells the combo class WHICH event(s) to sink.

Now you must of course instantiate your own combo class in your form and
pass in a specific control to it.

'inside my FORM'S class header...
'dimension my own combo class
dim fclsMyCombo as clsMyCombo

'inside my FORM'S class
Private Sub Form_Open(Cancel As Integer)
	'CREATE AN INSTANCE OF MY COOL COMBO CLASS
    	Set fclsMyCombo = new clsMyCombo
	'PASS A REFERENCE TO A SPECIFIC COMBO ON MY FORM
	fclsMyCombo.Init cboStates 'a combo displaying all the states in the
union
End Sub

THAT tells Access to dimension a class, create a class instance and WHICH
combo to pass off to the class instance.

NOW, with ALL THESE THINGS IN PLACE, if that combo AfterUpdate fires,
control will EVENTUALLY get to your class and your message box will display
it's message.

Notice that your form doesn't need any event stub in it for the control.
Even though the ONLY sub in your form's class is the Form_Open, the event
stub in your class will sink the event and the message box will display its
message.

I know full well that all this sounds like a LOT of work just to get control
into a class but after you've done it a few times it will be 2nd nature.
And of course you can then have a combo class that (see my other post)
handles the double click, opens a form, allows your user to edit the data,
and when the form closes, requeries the combo, all neatly encapsulated in a
class.  Plus all the other neato keenarino stuff you can imagine your combo
doing.

Hang in there.  It took me three tries, each six months apart to finally
"get" Shamil's stuff and understand Withevents.  Looking back I can't
understand why, but life is like that sometimes.

And no, the demos for the book are copyrighted by Wrox and I can't legally
distribute them, however there are many demos (by me of course) on
DatabaseAdvisors' web site.  I also have a bunch on my own site.  This stuff
is important, keep trying.

John W. Colby
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of Robert Gracie
Sent: Tuesday, January 13, 2004 8:47 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] Implements vs WithEvents


"Hmmmmmmm, As confusion sets in, he takes and stabs himself in the chest
with his keyboard...:-)"

  I'm trying to understand why the VBA DHB is teaching that using INTERFACE
inheritance classes (by way of the Implements Keyword and such) is much
better then simply using Event, RaiseEvent, and WithEvents... The problem,
for me at least, is getting my mind around the larger concept, and knowing
which structure to use and why.

  With my very limited understanding, the way they have presented, it seems
reasonable to go this route if only to help keep control over code
execution...

I guess I just need to keep pounding away at these concepts.......



Robert Gracie
www.servicexp.com






-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of John W. Colby
Sent: Tuesday, January 13, 2004 7:54 PM
To: Access Developers discussion and problem solving
Subject: [AccessD] Implements vs WithEvents


Charlotte,

>They have somewhat different purposes.

No, they have completely different purposes.

>Implements allows you to subclass an existing object/class and give it
additional or different functionality, so you can have a class Dog and a
subclass Spaniel that has a property Retriever.

Well... true but not exactly.  My issue with that statement is the term
subclass as you use it.  Yes you are subclassing the original but ONLY the
interface.  IMHO that is almost useless

Implements is an interface ONLY inheritance, i.e. it allows (forces) you to
start with the same public data and method DEFINITION but does not actually
inherit the behaviors (methods) themselves.  I.e. the code that runs in the
class that is being subclassed is NOT inherited into the subclass.

Access' inheritance (interface) is not very useful except as a template kind
of thing.  I find it useless enough that I personally don't bother.  On the
other hand Shamil is a proponent so it must have its uses.

When you move to VB.NET (or any of the other .net languages I believe) TRUE
inheritance is available.  I.e. if you have a method with a thousand lines
of code, when you subclass that object the new object inherits that code as
well as the data.  In our Implements subclassing, you would have to cut and
paste that thousand lines of code into the subclass.  Can you say
maintenance nightmare?

>You can intercept and respond to specific events when they happen in the
object.  Did the Bark event trigger?  WithEvents
lets you find out and respond from another class/object.

Exactly correct.  One of the MAJOR shortcomings of Access / VBA (in fact VB
prior to VB.Net) is that Inheritance is virtually non-existent, and even
worse, objects other than forms don't have a built in class that you can get
at to add functionality directly to.  What WithEvents really does is allow
you to build class wrappers for objects so that you can encapsulate
behaviors (functionality) in your class.  In essence you still have two
objects, the control (or even another class or OCX) and your class, but now
you can put all of the common functionality in your class along with the
event stubs of the control/class/ocx that generates the event.

This does not get you any further down the road towards inheritance, sadly
it simply doesn't appear to be possible in Access / VBA.  But it does
provide a huge step up in encapsulation.

John W. Colby
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of Charlotte
Foust
Sent: Tuesday, January 13, 2004 4:56 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] =function() in .onclick


They have somewhat different purposes.  Implements allows you to
subclass an existing object/class and give it additional or different
functionality, so you can have a class Dog and a subclass Spaniel that
has a property Retriever.  WithEvents allows you to monitor the events
of an object/class.  You can intercept and respond to specific events
when they happen in the object.  Did the Bark event trigger?  WithEvents
lets you find out and respond from another class/object.

Charlotte Foust

-----Original Message-----
From: Robert Gracie [mailto:Subscriptions at servicexp.com]
Sent: Tuesday, January 13, 2004 1:45 PM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] =function() in .onclick


Hello All,
 Since I'm just learning this type of programming technique I thought I
would ask a question...

  Why not use Implements instead of WithEvents?  My limited understand
of WithEvents is that they will not return functionality unit they (the
event) has completed processing... Since I'm just at the "door" so to
speak with learning how to use and incorporate these new (to me)
programming techniques into my work, I really want to start out using
the "best" technique...


Robert Gracie
www.servicexp.com


_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com


More information about the AccessD mailing list