Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Current Control Object ?

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,


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Current Control Object ?


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 what
advantages the use of the CCO intermediate object assignment give to
your code.

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


--
jamescox
------------------------------------------------------------------------
jamescox's Profile: http://www.thecodecage.com/forumz/member.php?userid=449
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=114782

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Current Control Object ?


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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Current Control Object ?


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

The next step that comes to mind - if your code for a group of the
userform controls is essentially identical - is to put that code in a
Private Sub and pass CCO as a parameter to the Sub. That way, you only
have one place to type the Sub code - and more importantly, only one
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 CCO
as the appropriate type of control instead of Object. It's not like it
was a mystery what type of control it is, because the Dim is in the
event code of a specific control.

If you use Object, VBA has to take a look at what the properties of
Me.ActiveControl are, figure out what kind of control it is and then
create an instance of that kind of control for your Set statement. That
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 and
properties as you type) will work.

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

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


--
jamescox
------------------------------------------------------------------------
jamescox's Profile: http://www.thecodecage.com/forumz/member.php?userid=449
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=114782

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Current Control Object ?

Thanks for your PoWs - I've saved this in my 'little' VBA library :-)


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

The next step that comes to mind - if your code for a group of the
userform controls is essentially identical - is to put that code in a
Private Sub and pass CCO as a parameter to the Sub. That way, you
only have one place to type the Sub code - and more importantly, only
one 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 CCO
as the appropriate type of control instead of Object. It's not like
it was a mystery what type of control it is, because the Dim is in the
event code of a specific control.

If you use Object, VBA has to take a look at what the properties of
Me.ActiveControl are, figure out what kind of control it is and then
create an instance of that kind of control for your Set statement.
That 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 and properties as you type) will work.

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

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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 Label Options - Forms Object vs Control Box Object Awrex Excel Discussion (Misc queries) 3 July 17th 09 07:10 PM
How to force selection of all current text in textbox control onentry? Chrisso Excel Programming 2 November 25th 07 09:55 PM
Object doesn't support current locale setting: 'setlocale' Martin Eckart[_2_] Excel Programming 0 May 17th 05 09:41 AM
Can I control Macro run automatically current worksheet? Microlong Excel Programming 2 March 2nd 05 12:39 PM
Can't save chart as object in current sheet Ann Scharpf Charts and Charting in Excel 1 December 3rd 04 11:24 PM


All times are GMT +1. The time now is 08:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright İ2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"