VBA ComboBox - What's so hot about the Change event?

dwooldridge

New Member
Joined
Dec 1, 2011
Messages
18
This thread is spawned from another where I am trying to work through some comboBox problems. It has been suggested that the Change event is the 'default' event or a comboBox or maybe the event of choice. I'm not real clear on what this means but it seems like there are problems concentrating on this event.

My testing seems to say:

1. If we clear the comboBox with VBA code like 'ComboBox.Clear' no events fire.

2. If we set the comboBox value with VBA code like 'ComboBox.Listindex = 2' then the Change and Click events fire.

3. If during execution we move to the control and select a value then the Enter, DropButt******* (if we used the drop down arrow), Change, Click, Dropbutt******* (if we used the drop down arrow),
BeforeUpdate, AfterUpdate, and Exit events fire.

Now wisdom out in the world is tellimg me to put my code into the Change event. But this doesn't make sense to me. What happens if we need to use the BeforeUpdate/AfterUpdate events? The Change event has already fired and our code has already executed - too late to cancel with the BeforeUpdate. Or am I missing something here?

I've seen comments about reading up on a flag to control execution of the event - maybe that relates.

1. Maybe you put the code that needs to be executed into say 'SubA'.

2. Then have a flag like 'boolean DoChangeEventLogic'.

3. When you want to set the comboBox via VBA you set the flag to TRUE and change ListIndex. The Change event fires and has the code 'If DoChangeEventLogic then call SubA'.

4. When you want to set the comboBox manually you navigate to the control. The Enter event fires where you set 'DoChangeEventLogic' to False. You select a new comboBox value (some way) and the Change event fires. The IF statement fails and no code is executed HOWEVER our comboBox does have a new value. The BeforeUpdate eventually fires and if we don't cancel then AfterUpdate and in that event we say 'Call SubA'.

Is this about it? And I guess if you don't need the BeforeUpdate/AfterUpdate then you can just get everything done in the Change event without worrying about flags.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This thread is spawned from another where I am trying to work through some comboBox problems. It has been suggested that the Change event is the 'default' event or a comboBox or maybe the event of choice. I'm not real clear on what this means but it seems like there are problems concentrating on this event.

My testing seems to say:

1. If we clear the comboBox with VBA code like 'ComboBox.Clear' no events fire.

2. If we set the comboBox value with VBA code like 'ComboBox.Listindex = 2' then the Change and Click events fire.

3. If during execution we move to the control and select a value then the Enter, DropButt******* (if we used the drop down arrow), Change, Click, Dropbutt******* (if we used the drop down arrow),
BeforeUpdate, AfterUpdate, and Exit events fire.

Now wisdom out in the world is tellimg me to put my code into the Change event. But this doesn't make sense to me. What happens if we need to use the BeforeUpdate/AfterUpdate events? The Change event has already fired and our code has already executed - too late to cancel with the BeforeUpdate. Or am I missing something here?

I've seen comments about reading up on a flag to control execution of the event - maybe that relates.

1. Maybe you put the code that needs to be executed into say 'SubA'.

2. Then have a flag like 'boolean DoChangeEventLogic'.

3. When you want to set the comboBox via VBA you set the flag to TRUE and change ListIndex. The Change event fires and has the code 'If DoChangeEventLogic then call SubA'.

4. When you want to set the comboBox manually you navigate to the control. The Enter event fires where you set 'DoChangeEventLogic' to False. You select a new comboBox value (some way) and the Change event fires. The IF statement fails and no code is executed HOWEVER our comboBox does have a new value. The BeforeUpdate eventually fires and if we don't cancel then AfterUpdate and in that event we say 'Call SubA'.

Is this about it? And I guess if you don't need the BeforeUpdate/AfterUpdate then you can just get everything done in the Change event without worrying about flags.


The reason software engineers get paid big bucks is because they understand how the data base is configured, and they know how to apply the programming language so that the logic makes the best use of the data base. So it basicallt all boils down to logic. Is it logical to use event code and if so, which event best suits the purpose.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top