Problem with AfterUpdate event in the controls in a UserForm

KolGuyXcel

Board Regular
Joined
Jun 29, 2018
Messages
147
In a UserForm, there are ComboBox1, ComboBox2 and TextBox1 with TabIndex set as 0, 1 and 2 respectively. TabStop is set to True for all. Only the AfterUpdate event for each of the ComboBoxes and the TextBox is defined and each one has a debug.print statement. When the UserForm is initialised, the immediate window is blank, but the boxes are correctly filled with relevant data reading from the relevant sheet with the Immediate Window blank. The focus (cursor blinking) is also on ComboBox1, as expected. When the Return Key is hit (without modifying anything in ComboBox1), the debug.print for ComboBox1 is getting executed in the Immediate Window and the focus shifts to ComboBox2 (text in ComboBox2 getting selected, though no code is written for that). Now when the Return Key is hit (without modifying anything in ComboBox2), the debug.print for ComboBox2_AfterUpdate is not getting executed, the Immediate Window is showing only debug.print of ComboBox1_AfterUpdate, and the focus shifts to TextBox1. The same is happening with TextBox1 i.e. if the Return Key is hit without modifying anything in TextBox1, the debug.print in TextBox1_AfterUpdate is not getting executed, the Immediate Window is still showing the execution of debug.print of only ComboBox1_AfterUpdate and the focus shifts to the "next" button. Shift+Return is also navigating the cursor in the UserForm as expected so are Tab and Shift+Tab, but none are executing the debug.print for the ComboBox2 and TextBox1. Even the debug.print in ComboBox1 is getting executed only the first time and not for subsequent changes of focus (in case someone intends to play around navigating the boxes using Shift, Tab and Return Keys). The AfterUpdate events are all getting executed when at least any one key other than Tab and Return Key (BackSpace, Delete etc.) is hit.

I need to trigger these AfterUpdate events even when nothing is getting modified or no other key is hit in either of the ComboBoxes or the TextBox.
What am I missing? Or do I have to resort to the KeyDown event (I would then have to deal with combinations of Shift Key, Tab Key and Return Key)?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If I understand what you're saying, you expect AfterUpdate to fire when you leave a textbox when its contents have not changed. No update (no change to contents) means no event. If you want something to happen whenever you leave the control, use the Exit event instead. AFAIK, it should have fired for controls that are updated by the Initialize event though. If it's not, you might want to step through initialize event code and see what's happening.
 
Upvote 0
If I understand what you're saying, you expect AfterUpdate to fire when you leave a textbox when its contents have not changed. No update (no change to contents) means no event. If you want something to happen whenever you leave the control, use the Exit event instead. AFAIK, it should have fired for controls that are updated by the Initialize event though. If it's not, you might want to step through initialize event code and see what's happening.
It worked. Though why the first debug.print statement is getting executed still remains a mystery!
 
Upvote 0
If you're referring to this statement
When the Return Key is hit (without modifying anything in ComboBox1), the debug.print for ComboBox1 is getting executed
then I'd say it's because the initialization event sets a control value. When form shows, that control has not been updated (just because you see a value in it does not mean it has been updated - it can be pending assuming it still has the focus). Then when you leave the control, it gets updated hence it fires. Stepping through your code may reveal exactly what is going on as you execute lines of code and look at userform reactions.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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