2

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 :)

2
  • How many values you have in Combo box ? Combo box change will happen only if more than 1 value in combo box Commented Jun 28, 2012 at 10:15
  • Thanks Logan; it has 3 values. Commented Jun 28, 2012 at 10:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick answer, Alex. Problem then is that the [Event Procedure] needs to be outside of the class, isn't? Should be a public function in a separate module.
No, if you add cbMyCombo.OnChange = "[Event Procedure]" to your class what you have should work
THANKS! By putting literally "[Event Procedure]" everything works. I misinterpreted it as a kind of placeholder for the name of the procedure linked with the event. Again, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.