Control Name

S

scott

If I have a label control called "lblMyLabel1". How can I get the name of
the label control when the label control is clicked?

I've got the OnClick event on the label control set to the CODE below, but
when it fires, it returns the name of the form. I need a way to return
"lblMyLabel1" without hard coding it.

CODE ******************

MSGBOX Me.Name ' renders myFormName
 
F

fredg

If I have a label control called "lblMyLabel1". How can I get the name of
the label control when the label control is clicked?

I've got the OnClick event on the label control set to the CODE below, but
when it fires, it returns the name of the form. I need a way to return
"lblMyLabel1" without hard coding it.

CODE ******************

MSGBOX Me.Name ' renders myFormName

I don't quite understand.
If your code is in a label's click event you already know the label's
name.
Anyway if the label is not tied to a text control, use:

MsgBox Me.ActiveControl.Name
 
S

scott

thanks. just for future knowledge, if the control and label are tied, would
the syntax be Me.ActiveControl.Label ?
 
S

scott

Actually Me.ActiveControl.Name doesn't return the name of an "untied" label,
it returns the active control. In my example, I have a form/subform. If the
label on the parent form is clicked, Me.ActiveControl.Name returns the
subform name. If I click a label placed in the subform header, it returns
the name of the first control in my continuous subform.

Any ideas? I realize that I know the label's name because I'm firing code
from it's onclick event. However, I'm trying to write a sub function that I
can automate the process without hard coding the label's name. I may end up
using a text box control as a label so I can trap the name, unless you have
a solution.
 
F

fredg

thanks. just for future knowledge, if the control and label are tied, would
the syntax be Me.ActiveControl.Label ?

No, because a label that is 'tied' to a text control has no events.
You would have to use an event elsewhere, such as in the text control
it is bound to.

MsgBox Me.ActiveControl.Controls(0).Name
 
F

fredg

Actually Me.ActiveControl.Name doesn't return the name of an "untied" label,
it returns the active control. In my example, I have a form/subform. If the
label on the parent form is clicked, Me.ActiveControl.Name returns the
subform name. If I click a label placed in the subform header, it returns
the name of the first control in my continuous subform.

Any ideas? I realize that I know the label's name because I'm firing code
from it's onclick event. However, I'm trying to write a sub function that I
can automate the process without hard coding the label's name. I may end up
using a text box control as a label so I can trap the name, unless you have
a solution.

I see nothing in your original post that indicates Main form and sub
form. Forms behave differently when used as a sub form.

So, to clarify this, you have an unattached label control on a
continuous form used as a sub-form, and you wish to get the label's
name from an event on the main form. Correct?

Perhaps this will help.

In a Module declare a global variable (in the declarations part of the
window):

Option Compare Database
Option Explicit
Public MyVariable as String

Save the module.

On the continuous form that is used as the sub form, code each
unattached label's click event:

MyVariable = "Write the actual label name here"

The last label to be clicked will have it's name stored in MyVariable.

You can then call it from any event in the Main form:

MsgBox MyVariable
 
A

AccessVandal via AccessMonster.com

Hi Scott,

Are you trying to change the Label Caption properties? It is not advisable to
change the Label Name.
scott wrote:
Any ideas? I realize that I know the label's name because I'm firing code
from it's onclick event. However, I'm trying to write a sub function that I
can automate the process without hard coding the label's name. I may end up
using a text box control as a label so I can trap the name, unless you have
a solution.

To use a message box to display a control name on the label Onclick event

MsgBox Me.YourLabelName.Name
MsgBox Me.YourLabelName.Caption 'Caption of your label
 
D

Dirk Goldgar

In
scott said:
If I have a label control called "lblMyLabel1". How can I get the
name of the label control when the label control is clicked?

I've got the OnClick event on the label control set to the CODE
below, but when it fires, it returns the name of the form. I need a
way to return "lblMyLabel1" without hard coding it.

CODE ******************

MSGBOX Me.Name ' renders myFormName

A label control can't receive the focus, so if you don't want to
hard-code the label name anywhere, you won't be able to do this without
some form of "subterfuge". You can use a text box, locked and formatted
to look like a label, or you can put a transparent command button over
the label, and set the name of the button or its Tag property so that
you can extract the information you want from it. You could get
extremely elaborate and use the label's MouseUp event to get the X and Y
coordinates of the mouse pointer, and check those coordinates against
the position and size properties of all the controls on the form -- or
all those you care about -- to see which one was clicked. There may be
other ways, of greater or lesser complexity, but there's no immediately
available property to do what you are asking.

If you explain what the purpose of your question is, we may be able to
suggest an alternative approach.
 
S

scott

Sorry for delayed response. I was using these labels as clickable buttons to
change the subform's recordsource (each label sorts on it's field). After
realizing the shortcomings of label controls, I went with textbox controls.

I was only trying the get the name of the control that was clicked so I
could write a function and not hard-code the function calling statement. I
apologise for not explaining better from the start.
 

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