Generic way to reference the control an event is occurring for?

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
348
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
My UserForm has a ton of controls and events, so it would be helpful if there was a guaranteed way to refer to the control whose event is currently running.

Application.Caller doesn't work, it returns Error 2023 in many cases. The ActiveControl option seems better suited, but frames complicate it. For example, I have a textbox that is on a frame within a frame, so to refer to that control when the event's code is running requires Me.ActiveControl.ActiveControl.ActiveControl.Name. Is there a better way?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What kind of event handling code are you using that doesn't know what control called it? Have you set up a class for a group of controls so all controls call the same event handler? I'm not quite getting why you would need code to determine what control called it.
 
Upvote 0
I should rephrase this as I just need the name of the event, including the control name, printed to the immediate window so I can see when each one executes. I went ahead and did it manually, but for future reference it would be good to know. For example, when the event named tbDate1_Enter executes, I wanted that same event name to print out with some kind of generic statement that I could easily copy and paste into each event, without needing to take additional steps to add in the specific event's name in the Print statement. Because I have to manually add in each events name into the statement, instead of something more convenient that automatically captures and prints that information.

The overall issue has to do with unexpected events triggering that are messing up a system I have in place to highlight the textbox with focus. It also slightly enlarges command buttons when focus moves to them. The problem has been that Enter & Exit commands don't work as expected in some cases. For the first and last control in a frame (by tab index #), one of the complementing events won't execute. Instead the frame's enter or exit event will execute, which can prevent intended code from running. Here's a brief form of the log I have of how events get executed (I highlighted the parts I didn't expect):
1670911132290.png


Of course, now that I somewhat understand the pattern it helps, but it still means that some events that seem like they should fire, aren't. tbSize1 has an Enter event, but never has an Exit. Instead frmEntry has it's exit, and so on. I'll just end up putting the sub calls in the frame events that are meant for a textbox.
 
Upvote 0
Additionally, I just realized that the Enter event for one of my command buttons is never fired again after it is activated the first time. Of course, its Exit never fired in the first place, but it's just even weirder that it can't activate either of them for the rest of the userform's duration.
 
Upvote 0

Forum statistics

Threads
1,215,316
Messages
6,124,228
Members
449,149
Latest member
mwdbActuary

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