Current Control Object ?

C

Charlotte E

When working with UserForms I normally assign the current control object
(CCO) to an object variable for ease of use, i.e.:

_________________________________
Private Sub OptionButton1_Click()

Dim CCO As Object
Set CCO = OptionButton1

If CCO.Value = False Then
CCO.ForeColor = 5
End If

End Sub
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Now I'm wondering if it is possible to set this object variable without
actually know the name of the control???

Maybe something like:

_________________________________
Dim CCO As Object
Set CCO = ThisControl.Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Hope I've made my explaning clear enough - or to put it short: Is there a
way of knowing what control the current control macro is for?


TIA,
 
J

jamescox

Well,

Dim CCO As Object
Set CCO = Me.ActiveControl

seems to work, because

CCO.Name

gives the right results on the test case I made up.

I'd really, really be interested in learning your thoughts on wha
advantages the use of the CCO intermediate object assignment give t
your code.

Prithee, please elucidate! (Prithee please! :)
 
C

Charlotte E

I'd really, really be interested in learning your thoughts on what
advantages the use of the CCO intermediate object assignment give to
your code.
Prithee, please elucidate! (Prithee please! :) )

Lazyness :)

If I have an userform with dozens of, say textboxes, which needs to be
controlled for correct entered values as the user types, and perhaps also
needs to be formattet along the way, I only need to make the code once.

Then I can use the same code in all 'TextboxYY_Change' modules, without
changing a single letter in the module!

It also makes it easier to change the code, if needed in the future, since I
only have to change a single line, and the rest of the code will now use the
new object instead.

So, no fancy trick here - just pure lazyness :)


CE
 
J

jamescox

Thought it might be that, but there was always a chance I was missin
some deeper advantage. However, don't get me wrong - I'm a big fan o
POLE (Path Of Least Effort). :Bgr

The next step that comes to mind - if your code for a group of th
userform controls is essentially identical - is to put that code in
Private Sub and pass CCO as a parameter to the Sub. That way, you onl
have one place to type the Sub code - and more importantly, only on
place to change / modify it. But, you may already be doing that...

Two comments - you would gain a bit of speed if you would declare CC
as the appropriate type of control instead of Object. It's not like i
was a mystery what type of control it is, because the Dim is in th
event code of a specific control.

If you use Object, VBA has to take a look at what the properties o
Me.ActiveControl are, figure out what kind of control it is and the
create an instance of that kind of control for your Set statement. Tha
doesn't take a whole lot of time, but if it has to be done often enough
it adds up. Also, if you declare it as the proper type of control
Intellisense (the feature of VBA that shows you allowable methods an
properties as you type) will work.

Finally, don't forget to use CCO = Nothing to clean up after you don'
need CCO any more.

(BTW, in terms of knowing 'who' called what code, don't forget th
.Caller method for regular subs and functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top