Reputation: 23
I'm trying to create a class that manages the events of a combo box in Access 2010. Here you have the code:
Class TestEvents
Public WithEvents cbMyCombo As Access.ComboBox
Public Property Get AssociatedCombo() As Access.ComboBox
Set AssociatedCombo = cbMyCombo
End Property
Public Sub cbMyCombo_Change()
MsgBox "Combo has changed!"
End Sub
Private Sub Class_Initialize()
Set cbMyCombo = Form_Form1.Combo1
End Sub
Form1 Code (Contains a combobox named Combo1)
Option Compare Database
Option Explicit
Private MyTestEvents As TestEvents
Private Sub Form_Load()
Set MyTestEvents = New TestEvents
MsgBox MyTestEvents.AssociatedCombo.Name
End Sub
When running the code, I get (as expected) a message with the combobox name (Combo1), so the TestEvents.AssociatedCombo property is pointing to the right object but nothing happens when I change the combo value. I would expect to get the message "Combo has changed".
Is there anything I'm doing wrong?
Thanks in advance for your help :)
Upvotes: 2
Views: 1882
Reputation: 175748
By default events sunk for a form control are not raised in VBA at all; to enable them (and subsequently enable your WithEvents
) you need to wire up each event you want to handle;
Set cbMyCombo = Form_Form1.Combo1 '//please put this in a property!
cbMyCombo.OnKeyDown = "[Event Procedure]"
cbMyCombo.OnBlaDeBla = "[Event Procedure]"
(Note that's the actual string value you need to set, not a placeholder/example)
You can also do this on the Events tab of the control properties.
Upvotes: 1