John Bodin
jbodin at sbor.com
Fri Feb 8 13:56:05 CST 2013
Hi John. Thanks for the code samples and step by step. I'm on board with what you have written and I created my little test form and it did function as expected on the double-click. Although the code was minimal, it was enough to make sense after several reads/re-reads. Making these like blackbox routines makes perfect sense - self-contained and you just need to know how to call them. I also played with the little drag and drop demo on the databaseadvisors site by Stuart, Drew & Darren which was a slightly different idea than I was going for, but will definitely keep in mind for future development. I saw this demo in a thread here http://www.access-programmers.co.uk/forums/showthread.php?t=221317. This showed a nice way to drag from one text box, with a visual icon while dragging, and drop to another text box and acting upon the 'drop'. This is what I am trying to achieve, obviously with my custom routines and options when the drop occurs. The class idea seems to make sense here, just having issues (and I know I'm restating myself) in generically determining /referencing the Button/Shift/X/Y parameters. Thanks for the time taken in all your responses! John ------------------------------ Message: 2 Date: Thu, 07 Feb 2013 09:33:23 -0500 From: John W Colby <jwcolby at gmail.com> To: Access Developers discussion and problem solving <accessd at databaseadvisors.com> Subject: [AccessD] Doin the class thing - was Re: Generic Procedure on form controls for Drag and Drop Message-ID: <5113BB33.7020709 at gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed 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 On 2/6/2013 1:55 PM, John Bodin wrote: > Hello, > > > > New to this list and need some guidance on a form I've developed (I posted > this on LinkedIn and got a few links with good information, but still having > issues). > > > > I have a grid of many text boxes on a form in an Access 2003 app that I fill > from a table upon opening the form as well as when the user changes a date > box. I add some generic procedure calls on the fly to each text box where I > pass the control to the generic function (for instance, I'll add a > double-click event to text box "Txt101" as "=TxtBoxDblClick([Txt101])". This > will pass the control to my function TxtBoxDblClick so I can react to it.) > This all works fine for all text boxes and I reference just one (same) > routine for each control. > > I'm trying to experiment with Drag and Drop and found some code that I can > get to work if at Design time, I add three event procedures to a text box > control (Mouse Down/Up/Move). If I do this to two different controls, > creating 3 event procedures for each at design time, I can successfully drag > and drop between the two controls. So the drag and drop code looks like it > works. > > My problem is, I want to have 3 generic routines like my TxtBoxDblClick > function, that I can add on the fly to the On Mouse Down/Up/Move events and > I can't figure out the syntax and/or code to make this happen. I can add the > custom Functions no problem, but I know I need to be able to deal with the > Button, Shift, X & Y parameters somehow. I can pass the control to the > custom function, but am unable to reference the Button, Shift, X & Y > parameters. I'm guessing I'll need to create some type of class possibly? If > so, can someone provide some sample code on how to do this what the calls > would be? Thanks for any ideas. John > > > ------------------------------ Message: 3 Date: Thu, 07 Feb 2013 10:17:49 -0500 From: John W Colby <jwcolby at gmail.com> To: Access Developers discussion and problem solving <accessd at databaseadvisors.com> Subject: Re: [AccessD] Doin the class thing - clsDNDText Message-ID: <5113C59D.2090404 at gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed 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 > ------------------------------ Message: 4 Date: Thu, 07 Feb 2013 10:29:48 -0500 From: John W Colby <jwcolby at gmail.com> To: Access Developers discussion and problem solving <accessd at databaseadvisors.com> Subject: [AccessD] Doin the class thing - frmDNDText Message-ID: <5113C86C.2080408 at gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed The code for the form's class is simple. In the form's header we create an instance of the class for every text box we want to handle. Notice that I already have a disclaimer that this is the 'quick and dirty' way of doing things. This works but there is a better way which we will discuss once you have absorbed this method. ' 'The quick and dirty way to do this ' Private cDNDTxt2 As clsDNDText Private cDNDTxt4 As clsDNDText Private cDNDTxt6 As clsDNDText In the form's Open event we initialize each of the instances of this class, setting the pointer to each instance using the new keyword and then passing in a pointer to one of the text boxes in the mInit method. 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 and finally, not included in the original post we want to cleanup in the form's close event Private Sub Form_Close() Set cDNDTxt2 = Nothing Set cDNDTxt4 = Nothing Set cDNDTxt6 = Nothing End Sub How much simpler can it be? Let's discuss this and then we can move on to the next step. -- John W. Colby Reality is what refuses to go away when you do not believe in it ------------------------------ Message: 5 Date: Thu, 7 Feb 2013 10:34:01 -0500 From: John Colby <jwcolby at gmail.com> To: Access Developers discussion and problem solving <accessd at databaseadvisors.com> Subject: Re: [AccessD] Any book recommendations - Event Procedures/Programming Message-ID: <CAA_iUE9X2DZw1YRau96awkYN9JztQ-oVW9c6O5cpuC4G-gxybA at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Classes and events is the highly guarded secret of the elite few. ;) If everyone knew about them then how could we claim to be elite? jwcolby On Tue, Feb 5, 2013 at 10:16 AM, jack drawbridge <jackandpat.d at gmail.com>wrote: > Just curious if anyone has a book(s) (or site(s)/video(s)) recommendation > for Event Procedures generally. I know John C has started many tutorials > and expressed the importance of classes and events, but I find info (Google > search based) sparse at best. I have not found a site that addresses > classes with the same level and description of JC. (Was the stuff he > tempted us with (2009) ever get publish/collected and saved. I have found > some things by Chris O'Brien (on another forum) related to soft coded > events. > > It's more a curiosity than a need. I'm retired, not consulting, but have a > recurring interest and thought I should ask. > > Thanks in advance. > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com > -- John W. Colby Colby Consulting ------------------------------