By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

Get the source element in event handler. Equivalent of "This" keyword in VBA

P: 2
Hi there!

Suppose you have N checkboxes on a MSAccess form and you want that every from them to show it's name on it's OnClick event.

So you do:
Private Sub checkbox1_Click()
MsgBox checkbox1.Name
End Sub

In other programming languages I would write
Private Sub checkbox1_Click()
MsgBox This.Name
End Sub

So is there such word equvalent in VBA or at least something that gives the source element in the body of the event handler?
The example here is trivial but in a real situation I must write additional function and pass to it the current element, which is not so bad .... but anyway ... ?

Thanks in advance!

Sep 21 '07 #1
Share this Question
Share on Google+
5 Replies

Expert 2.5K+
P: 2,653
Hi, sunsande.

To the best of my knowledge VBA does not support this feature. If you want to write a common routine, then you may create sub receiving object ID as argument. Something like this.

Expand|Select|Wrap|Line Numbers
  1. Private Function CommonHandler(ID AS <SomeType>)
  2. ...........................
  3. End Sub
And set OnClick event of checkBoxes to
"=CommonHandler(<the checkBox ID>)"
Sep 21 '07 #2

Expert 2.5K+
P: 3,532
This will do it, where YourCheckBox is the actual name of said checkbox:
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourCheckBox_Click()
  2.   MsgBox Screen.ActiveControl.Properties("Name")
  3. End Sub
You could, of course, write a function to do this, but you'd still have to call the function from each control's Click event, so what's the point? You'd simply be trading one line of code for another line of code.

Welcome to TheScripts!

Linq ;0)>
Sep 21 '07 #3

Expert Mod 15k+
P: 31,494
Intelligent question Sunsande :)
Objects typically reference themselves as "Me" in VBA. Unfortunately, the situation with controls on a report or form type object is that Me still refers to the parent (Report or Form). I think Linq's answer is probably the best, which is to use Me.ActiveControl. This, unfortunately, is not 100% reliable as I believe there exists the possibility to trigger events even without setting the control as active (EG. AfterUpdate when control set in VBA code).
Hope this is helpful.
Sep 21 '07 #4

P: 2
Solala :) you are really fast! Thank you all!

Your answers made it clear for me!

I know that my example was not so meaningful. It is quite trivial and in this case the usage of this can be easily changed in the common Function like this:
Expand|Select|Wrap|Line Numbers
  1. Function CommonHandler (sId as String) or even directly (chk as CheckBox)
  2.     Dim chk as CheckBox
  3.     Set chk = Me.Controls("sId")
  4. ........' do the job
  5. End Function
For coding it is not that hard but I see at least 2 shortcomings. It is error prone - you should always look carefully if you pass the right name or object name, and thus you normally need to write some error checking in the common event handler function ...
Whereas this is always this - too hard to be misspelled and easy to find and fix.

On the other hand if you want to attach the same event handler function on different type of controls you should cast parameter of the common function to Variant or do some additional checks inside it ...

Very often I implement the following search form:
About 10 check boxes, which switch on/off some search criteria and which also act immediately (filtering the result set). I set the "filters" on the .Tag properties of these controls. For every control I have then:

Expand|Select|Wrap|Line Numbers
  1. Public Sub chk_OnClick ()
  2.     CommonHandler (chk.Tag, chk.Value)
  3. end sub
It is boring to type this 10 times only with this little change (the name chk). With this it will be something like:
Expand|Select|Wrap|Line Numbers
  1. Public Sub chk_OnClick ()
  2.     CommonHandler (this.Tag, this.Value)
  3.     'or why not just CommonHandler () inside the function body we can then     use this directly.  ....
  4. end sub

May be they will implement "this" keyword in some of the next versions of Access.

Thank you all one more time!

Best regards,
Sep 21 '07 #5

Expert Mod 15k+
P: 31,494
Very interesting comment & I agree with your reasoning and style of programming. I doubt they'll introduce a This reference exactly though, as they already have a Me reference which is similar but doesn't support controls as such (As per my previous post #4). I agree that it would be useful though. I too like to program that way but I've got used to it not being available ;)
Sep 21 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.