|
Question : WithEvents using ActiveX controls
|
|
I suspect this one to be challenging to answer. I am trying to trap the update events on an unbound form using WithEvents. My code has been successful for all but one control type (DateTimePicker ActiveX control). I need to be able to trap the update event of this control. Also in my code I have declared and instanciated new class objects for each control type. This does not seem to me to be an efficient way to do things, but was all I could figure out. If someone knows of a more efficient way, feel free to post it, otherwise here is my current code. I have noted the line of failure.
'*********Clas Module Code***************** Option Compare Database Option Explicit 'This class is used in conjunction with unbound forms to detect when a user has editied data Public WithEvents TextBox As TextBox Public WithEvents ComboBox As ComboBox Public WithEvents OptionBtn As OptionButton Public WithEvents CheckBox As CheckBox Public WithEvents DateTimePic As DTPicker
Private Sub TextBox_AfterUpdate() Call TestMessage End Sub Private Sub ComboBox_AfterUpdate() Call TestMessage End Sub Private Sub OptionBtn_AfterUpdate() Call TestMessage End Sub Private Sub CheckBox_AfterUpdate() Call TestMessage End Sub Private Sub DateTimePic_Updated() Call TestMessage End Sub Private Sub TestMessage() MsgBox "Test" End Sub '*********Clas Module Code Ends*************
'*********Form Code ************ Private TextBox() As New clsTrapUpdates Private ComboBox() As New clsTrapUpdates Private OptionBtn() As New clsTrapUpdates Private CheckBox() As New clsTrapUpdates Private DateTimePic() As New clsTrapUpdates
Private Sub Form_Load() Dim ctl As Control Dim txtCount As Long Dim cboCount As Long Dim optCount As Long Dim chkCount As Long Dim dtpCount As Long
For Each ctl In Controls Select Case ctl.ControlType Case acOptionButton optCount = optCount + 1 ReDim Preserve OptionBtn(optCount) Set OptionBtn(optCount).OptionBtn = ctl ctl.AfterUpdate = "[Event Procedure]" Case 119 'This is the numerical constant for ActiveX control DTPicker dtpCount = dtpCount + 1 ReDim Preserve DateTimePic(dtpCount) Set DateTimePic(dtpCount).DateTimePic = ctl <---Runtime Error 13 'Type Mismatch' ctl.Update = "[Event Procedure]" Case acTextBox txtCount = txtCount + 1 ReDim Preserve TextBox(txtCount) Set TextBox(txtCount).TextBox = ctl ctl.AfterUpdate = "[Event Procedure]" Case acCheckBox chkCount = chkCount + 1 ReDim Preserve CheckBox(chkCount) Set CheckBox(chkCount).CheckBox = ctl ctl.AfterUpdate = "[Event Procedure]" Case acComboBox cboCount = cboCount + 1 ReDim Preserve ComboBox(cboCount) Set ComboBox(cboCount).ComboBox = ctl ctl.AfterUpdate = "[Event Procedure]" End Select Next ctl Set ctl = Nothing End Sub
'*********Form Code Ends*********
|
|
Answer : WithEvents using ActiveX controls
|
|
Well - you're creating a generic method of hooking in to form controls. And that's essentially what you'll accomplish. I personally have never had need to do this - and so wouldn't want to suggest anything different. I hook into controls and objects events as and when I need them.
I'm a massive fan of maintaining versatility and genericness - but in general I find that a reasonable section of reusable code tapped into and appended as required is enough. (Sometimes routines made into a large library of classes are very generic and work well - but by the time the code's gone to the bother of instantiating it and setting necessary properties - little, if anything, has been gained. I'm not saying that's the case here - just that I've never felt the need).
As for its efficiency I wouldn't worry about it too much. If better solutions exist - they usually present themselves over time. It's important to have a starting point - don't you find? :-)
|
|
|
|