views:

742

answers:

3

I have a Word userform with 60+ controls of varying types. I would like to evaluate the form every time a control_change event is triggered and change the enabled state of the form's submit button. However, I really don't want to write and maintain 60 on change event handlers.

Could anyone help me out?

Cheers

+2  A: 

In that case you have few options, because event handlers cannot be shared in VBA/VB6

Option 1: Use a central handling function which is called from every event handler.

Sub Control1_ChangeEvent()
  CommonChangeEvent // Just call the common handler, parameters as needed
End Sub

Sub Control2_ChangeEvent()
  CommonChangeEvent
End Sub
...
Sub CommonChangeEvent(/* Add necessary parameters */)
  //Do the heavy lifting here
End Sub

Option 2: Organize your controls in control arrays.

Sub TextBox_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

Sub OtherControlType_ChangeEvent(Index As Integer)
  CommonChangeEvent
End Sub

Combining both options your total event handler count will shrink considerably and the remaining handlers are just brainless stubs for the one true event handler.

DR
Option 2 is not an option in this case. VBA does not support control arrays.
Gary McGill
+2  A: 

You can create an event-sink class that will contain the event-handling code for all of your controls of a particular type.

For example, create the a class called TextBoxEventHandler as follows:

Private WithEvents m_oTextBox as TextBox

Public Property Set TextBox(ByVal oTextBox as TextBox)
    m_oTextBox = oTextBox
End Property

Private Sub m_oTextBox_Change()
    ' Do something
End Sub

Now you need to create & hook up an instance of that class for each control of the appropriate type on your form:

Private m_oCollectionOfEventHandlers As Collection

Private Sub UserForm_Initialise()

    Set m_oCollectionOfEventHandlers = New Collection

    Dim oControl As Control
    For Each oControl In Me.Controls

        If TypeName(oControl) = "TextBox" Then

            Dim oEventHandler As TextBoxEventHandler
            Set oEventHandler = New TextBoxEventHandler

            Set oEventHandler.TextBox = oControl

            m_oCollectionOfEventHandlers.Add oEventHandler

        End If

    Next oControl

End Sub

Note that the reason you need to add the event handler instances to a collection is simply to ensure that they remain referenced and thus don't get discarded by the garbage collector before you're finished with them.

Clearly this technique can be extended to deal with other types of control. You could either have separate event handler classes for each type, or you could use a single class that has a member variable (and associated property & event handler) for each of the control types you need to handle.

Gary McGill
A: 

Been developing in Excel for years and wanted to say Thank you for having a solid reference to WithEvents. For as many years as I've used it, I always find myself having to go back and research the basics.

HelpExcel.com

Eddie