Capture Control Name Clicked

G

Guest

I am looking to sort a subform based on the column of data clicked. In the
past, I have created a separate but nearly identical OnClick event for each
label. In the interest of condensing code, since i have 25 columns, which may
or maynot be shown depending on the SQL string built through code, and I
don't want 25+ more Subs, all nearly identical...

I would like to set the same function as the OnClick event of every label,
and have the name of the label be passed to the function, from which I can
get the column selected through a MID function.

I can get this to work if I use Text Boxes instead of labels (through
..ActiveControl), but then they react like text boxes (surprise!) with the
cursor, highlighting the text, etc..., which I don't like.

I can get it to work with buttons the same way, but buttons have more of an
inner margin than labels, too much for me to realistically work with, and I
can't change the button background color to match the interface.

Labels clicked don't register as an ActiveControl, so I can't use this method.

Anyone know of a way to pick out which label/control was clicked other than
the methods above?
 
G

Guest

How about putting a transparent command button over each label and using the
button's Click event property to call the function, passing the name of the
relevant label in each case into the function, e.g.

=MyFunction("MyLabel")

As far as the user is concerned they are clicking the label, but in fact are
clicking the transparent button, which is totally invisible to them. Add
each button in the normal way and size it to fit over the label, then set its
Transparent property to True (Yes in the properties sheet).

Ken Sheridan
Stafford, England
 
B

Bob Quintal

How about putting a transparent command button over each label and
using the button's Click event property to call the function,
passing the name of the relevant label in each case into the
function, e.g.

=MyFunction("MyLabel")

As far as the user is concerned they are clicking the label, but
in fact are clicking the transparent button, which is totally
invisible to them. Add each button in the normal way and size it
to fit over the label, then set its Transparent property to True
(Yes in the properties sheet).

Ken Sheridan
Stafford, England

Even better is to use a single command button over the entire set of
labels in hte header of the form, then using the mousedown event to
determine the position of the mouse. Each label's left property,
converted to the twips, and put in descending order in a select case
statement, would allow determination of which label was clicked.

private sub cmdSort_OnMousedown(....)
const TPI = 1440 'twips per inch
dim strField as string
Select case X / TPI
case > 3
' over label 4
strField = "UM"
case > 2.25
'over third label
strField = "QTY"
case > 1.5
'over label 2
strField = "PartName"
case > 0.75
'over label 1
strField = "PartID"
end select
me.orderby = strfield
me.orderbyon = true
end sub

Someone with a little free time could even code a routine that puts
the control.controlsource and control.left values into an array then
sorts it on form open, then walks the array in the mousedown event
looking for the right value.
 
G

Guest

Had forgotten about transparent buttons. Thanks!

Ken Sheridan said:
How about putting a transparent command button over each label and using the
button's Click event property to call the function, passing the name of the
relevant label in each case into the function, e.g.

=MyFunction("MyLabel")

As far as the user is concerned they are clicking the label, but in fact are
clicking the transparent button, which is totally invisible to them. Add
each button in the normal way and size it to fit over the label, then set its
Transparent property to True (Yes in the properties sheet).

Ken Sheridan
Stafford, England
 

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