Question : MS Access global event handler

This concerns MS Access 2003 VBA.
I have 336 textboxes on a form.  I want them all to respond to a double-click event in the same fashion--and perhaps in the future to other events.  I'd prefer not to create 336 separate events in the form's module, one for each textbox.  Sample code for this type of solution, for Excel, can be found at:
http://j-walk.com/ss/excel/tips/tip44.htm

I cannot get my adapted code to work in Access.  It compiles without errors--but the double-click event does not get handled.  What am I doing wrong?  Code to follow:

in a class module named clsTextboxGroup
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
'clsTextboxGroup
'-------------------------------------------------------------------
Option Compare Database
Option Explicit
Public WithEvents TextBoxGroup As Access.TextBox

Private Sub TextboxGroup_DblClick(Cancel As Integer)
    MsgBox "Hello from " & TextBoxGroup.Name
    'I will put in more substantive events after I get this working
End Sub
'-------------------------------------------------


In the module of the form with the 336 textboxes

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
'the form's module
Option Compare Database
Option Explicit
Dim MyTextboxes() As New clsTextBoxGroup

Private Sub Form_Load()
Call InitMyTextboxEvents
end sub
'---------------------------------------------------
Public Sub InitMyTextboxEvents()
Dim TextboxCount As Integer
Dim ctl As Control

'   Create the Textbox Objects
TextboxCount = 0
For Each ctl In Forms("frmWeek").Detail.Controls
    If ctl.ControlType = acTextBox Then
        If ctl.Name Like "*booking*" Then 'snag only the required textbox controls
            TextboxCount = TextboxCount + 1
            ReDim Preserve MyTextboxes(1 To TextboxCount)
           Set MyTextboxes(TextboxCount).TextBoxGroup = ctl
        End If
    End If
Next ctl

end sub

Answer : MS Access global event handler

I haven't worked with classes all that much, but I do see a couple of things.

1. You've created a variable in your class module, TextBoxGroup but it doesn't point to anything.

You need to add:

Public Property Set TextBoxGroup(txt as Textbox)

   Set TextBoxGroup = txt

End Sub

to your class.

2. Access has an optimization built-in that if it doesn't see '[Event Procedure]' for an event property, it doesn't raise an event.  So you need to set that.  You can do this in the class Set:


Public Property Set TextBoxGroup(txt as Textbox)

    Set TextBoxGroup = txt
    TextBoxGroup.OnDblClick = "[Event Procedure]"

End Sub

3. Last, as Helen has said, I don't think you can use an array like that.  instead, you need to have your class handle a single text box and then use a collection in the form to build an array of controls.  I'll see if I can't dig up some code or someone who has worked with classes a lot more.

JimD.

Random Solutions  
 
programming4us programming4us