Repetitive OnClick Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form where I have 112 (!) labels where I want the same snippet of
code to run when the label is clicked. (It does some format stuff to the
label.) I have it working OK, but I have several pages of almost-identical
code as a result, with the only difference being the label name. Can anyone
suggest a way to streamline this, as in "any time ANY of this group of
controls is clicked, do THIS to it?" I've been scratching my head for a
couple days now but no inspiration has come.
 
LarryP said:
I have a form where I have 112 (!) labels where I want the same
snippet of code to run when the label is clicked. (It does some
format stuff to the label.) I have it working OK, but I have several
pages of almost-identical code as a result, with the only difference
being the label name. Can anyone suggest a way to streamline this,
as in "any time ANY of this group of controls is clicked, do THIS to
it?" I've been scratching my head for a couple days now but no
inspiration has come.

You can put the relevant code into a function in the General section of
the form's module, and set the OnClick properties of all the labels to
an expression that calls the function and passes the name of the label,
or the label control itself, to the function as an argument.

It would be easier if they were command buttons or text boxes instead of
labels, because a label can't receive the focus, and so you can't use
the form's ActiveControl property to identify which label has been
clicked. That's why you have to pass the name of the control, or the
control itself, to the function. So you can't just select all the
labels, bring up their combined property sheet, and enter

=MyFunction()

on the OnClick property line. Instead, you have to make a different
entry on each label's property sheet:

=MyFunction([Label1])

Then your code for the function is something like:

Function MyFunction(lbl As Access.Label)

With lbl
' .. work with properties of the label
End With

End Function
 
Can anyone
suggest a way to streamline this, as in "any time ANY of this group of
controls is clicked, do THIS to it?"

Private sub lblOne_Click()
RespondToClick lblOne
End Sub

Private Sub lblTwo_Click()
RespondToClick lblTwo
End Sub

Private Sub etc...


Private Sub RespondToClick( _
CurrentLabel As Control _
)

If TypeOf CurrentLabel Is Label Then
' do something useful with it here

Else
' debug.print "Called with wrong type of control"

End If

End Sub


Unfortunately you can't use the ActiveControl property because labels
cannot hold the input focus. Hope that helps



Tim F
 
Thanks, Dirk (and Tim). I'll experiment with this approach.

Dirk Goldgar said:
LarryP said:
I have a form where I have 112 (!) labels where I want the same
snippet of code to run when the label is clicked. (It does some
format stuff to the label.) I have it working OK, but I have several
pages of almost-identical code as a result, with the only difference
being the label name. Can anyone suggest a way to streamline this,
as in "any time ANY of this group of controls is clicked, do THIS to
it?" I've been scratching my head for a couple days now but no
inspiration has come.

You can put the relevant code into a function in the General section of
the form's module, and set the OnClick properties of all the labels to
an expression that calls the function and passes the name of the label,
or the label control itself, to the function as an argument.

It would be easier if they were command buttons or text boxes instead of
labels, because a label can't receive the focus, and so you can't use
the form's ActiveControl property to identify which label has been
clicked. That's why you have to pass the name of the control, or the
control itself, to the function. So you can't just select all the
labels, bring up their combined property sheet, and enter

=MyFunction()

on the OnClick property line. Instead, you have to make a different
entry on each label's property sheet:

=MyFunction([Label1])

Then your code for the function is something like:

Function MyFunction(lbl As Access.Label)

With lbl
' .. work with properties of the label
End With

End Function

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top