[AccessD] Doin the class thing - clsDNDText

John W Colby jwcolby at gmail.com
Thu Feb 7 09:17:49 CST 2013


OK, let's discuss what is going on.

clsDNDText is a class which I call an object wrapper.  It wraps an access object (a text box in this 
case) and then uses event sinks to service the events of the wrapped object.  So this class wraps a 
text box.  Notice in the header of the class the line:

Private WithEvents mtxt As TextBox

This dimensions a text box variable private (cannot be seen outside of the class) and informs VBA 
that the class will be sinking events (the WithEvents keyword).

Next we have a constant which holds the text "[Event Procedure]"

Private Const cEvProc As String = "[Event Procedure]"

It is good programming practice to as much as possible have a class self contained, not depending on 
outside resources to perform its function.

Next we have termination code.  When a class unloads it calls its terminate event if one exists.  In 
this case it simply "unhooks" the text box, setting the pointer to nothing.  We do that because it 
is possible to prevent a form from closing when objects on the form have pointers to them that are 
not cleaned up.

Private Sub Class_Terminate()
     Set mtxt = Nothing
End Sub

We then have an Init function which is public, and allows us to pass in a pointer to a text box.  
mInit then stores that pointer in our class header.  This allows the class to sink any and all 
events for the passed in object (text box).  Notice that we immediately place the cEvProc into the 
OnDblClick property of the text box.  It is a little known fact that it is the very existence of 
that text in the property that causes VBA to start raising events for the object.  By specifically 
placing that code in that property in our code, we do not need the developer to double click the 
property for us.  In this case we have only hooked the OnDblClick event however we could add others 
by adding a similar line of code for other events.

Function mInit(ltxt As TextBox)
     Set mtxt = ltxt
     mtxt.OnDblClick = cEvProc
End Function

And finally, the guts of the thing, the very reason for doing all this, we have the event sink 
itself.  This code is entered when the event fires for the specific text box which this class 
instance wraps.  IOW if we have several instances of this class, one for TextBox 2, TextBox4 and 
TextBox6, then when you double click in TextBox4, the event in the instance servicing that text box 
will sink the event and do something with it.  In this case I am simply poppigg up a message box to 
display the name of the control.

Private Sub mtxt_DblClick(Cancel As Integer)
     'Do something here
     MsgBox mtxt.Name & ".DblClick"
End Sub

John W. Colby

Reality is what refuses to go away
when you do not believe in it

On 2/7/2013 9:33 AM, John W Colby wrote:
> John,
>
> Create a little test database.
> Create a form
> Drag and drop 3 text boxes out.  When I did this they ended up with the names Text2, Text4 and 
> Text6.  Make sure that your text boxes are named that or the code won't work as it is.
>
> Open the form's code window and paste this in:
>
> '#####################################
> Option Compare Database
> Option Explicit
>
> '
> 'The quick and dirty way to do this
> '
> Private cDNDTxt2 As clsDNDText
> Private cDNDTxt4 As clsDNDText
> Private cDNDTxt6 As clsDNDText
>
> Private Sub Form_Open(Cancel As Integer)
>     Set cDNDTxt2 = New clsDNDText
>     cDNDTxt2.mInit Text2
>
>     Set cDNDTxt4 = New clsDNDText
>     cDNDTxt4.mInit Text4
>
>     Set cDNDTxt6 = New clsDNDText
>     cDNDTxt6.mInit Text6
>
> End Sub
> '#####################################
>
> Next insert a class module.  The 'how' changes between 2003 annd 2007 so I am leaving that up to you.
>
> In that class module insert the following:
>
> '#####################################
> Option Compare Database
> Option Explicit
>
> Private WithEvents mtxt As TextBox
> Private Const cEvProc As String = "[Event Procedure]"
> Private Sub Class_Terminate()
>     Set mtxt = Nothing
> End Sub
>
> Function mInit(ltxt As TextBox)
>     Set mtxt = ltxt
>     mtxt.OnDblClick = cEvProc
> End Function
>
> Private Sub mtxt_DblClick(Cancel As Integer)
>     'Do something here
>     MsgBox mtxt.Name & ".DblClick"
> End Sub
> '#####################################
>
> Save everything.  Name the class clsDNDText.  The form name doesn't matter.
>
> Open the form.  Double click in each text box.
>
> John W. Colby
>
> Reality is what refuses to go away
> when you do not believe in it
>



More information about the AccessD mailing list