OnClick event - pass this control to method (1 Viewer)

mcalex

Registered User.
Local time
Today, 12:28
Joined
Jun 18, 2009
Messages
141
Solved: OnClick event - pass this control to method

Hi all

how do i tell a method to do something to a clicked control?

I have a form with a bunch of labels and a bunch of buttons, and I want to run a method whenever one of these controls is clicked:
Code:
Private Sub manipulateControl(ByRef ctrl As Control)
  If (TypeOf ctrl Is Label) Then
    with ctrl
      ' do stuff that changes the clicked label
    end with
  ElseIf (TypeOf ctrl Is Button) Then
    with ctrl
      ' do stuff to the clicked button
    end with
  End If
End Sub

So far, so good, but how do i code the OnClick event method for each control? Currently I am trying to use ActiveControl:
Code:
Private Sub Label74_Click()
  manipulateControl Me.ActiveControl
End Sub
but am getting 'Runtime Error 2474: The expression you entered requires the control to be in the active window'. The control is in the active window (coz I just clicked on it).

What should I be doing?

tia,
mcalex
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 05:28
Joined
Jan 22, 2010
Messages
26,374
You can subclass the controls but that's quite involving. You might find something in the code repository regarding this.

With your current method here are a few notes.

The IF part can be a select case instead:
Code:
Select Case ctrl.ControlType
     Case acLabel
          ....do something
     Case acTextbox
          ....do something
end select
Notice the ControlType property introduced and the label/textbox constants used.

When it comes to calling the function you simply do this:
Code:
manipulateControl Me.[COLOR=Red][B]NameOfControl[/B][/COLOR]
 

mcalex

Registered User.
Local time
Today, 12:28
Joined
Jun 18, 2009
Messages
141
Thanks for ControlType heads up vbaInet. I'll fix that bit straightaway.

So it looks like I won't get out of writing different code for each _Click method?

Do you know why ActiveControl doesn't work?

ta
mcalex
 

vbaInet

AWF VIP
Local time
Today, 05:28
Joined
Jan 22, 2010
Messages
26,374
With subclassing you can but as mentioned, it is involving. If you don't have more 30 labels then it would be pointless.

A lable is never an active control because it doesn't ever get focus.
 

mcalex

Registered User.
Local time
Today, 12:28
Joined
Jun 18, 2009
Messages
141
vbaInet said:
A lable is never an active control because it doesn't ever get focus.
sheesh, i knew that :eek:

ahhhh, so all i have to do is change my labels to textboxes, and then:
Code:
manipulateControl Screen.ActiveControl
works

thank you

i'm now off to change 50-odd labels into textboxes :rolleyes:
 

vbaInet

AWF VIP
Local time
Today, 05:28
Joined
Jan 22, 2010
Messages
26,374
If I were you I would avoid ActiveControl if you can send the control via the function.
 

mcalex

Registered User.
Local time
Today, 12:28
Joined
Jun 18, 2009
Messages
141
oh. :(

but that brings me right back to the original problem. How do I reference the clicked Control to pass it to the function? I don't know how to do subclassing in vba.

What makes ActiveControl 'avoidworthy'? If I've just clicked on the Control, doesn't that guarantee that that Control is active?
 

vbaInet

AWF VIP
Local time
Today, 05:28
Joined
Jan 22, 2010
Messages
26,374
Try rereading my first post. The second block of code covers exactly how to send a reference of the active control.

ActiveControl relies on the object that currently has focus. An error could cause another object to gain focus and there are other factors.
 

ChrisO

Registered User.
Local time
Today, 14:28
Joined
Apr 30, 2003
Messages
3,202
Common Labels behave okay for the Click event.

See attached, the Labels are at the bottom of the screen.
 

Attachments

  • CommonControlTest.zip
    51.8 KB · Views: 129

vbaInet

AWF VIP
Local time
Today, 05:28
Joined
Jan 22, 2010
Messages
26,374
Ah, I knew Chris would like this sort of question. Have you also got this example in the Code Repository section?
 

ChrisO

Registered User.
Local time
Today, 14:28
Joined
Apr 30, 2003
Messages
3,202
Maybe, I don’t remember.

We could also sub-class Labels for other things but I would not recommend it.
Best to keep things simple.

See attached.
 

Attachments

  • db3.zip
    69 KB · Views: 117

mcalex

Registered User.
Local time
Today, 12:28
Joined
Jun 18, 2009
Messages
141
And Solved again! :)

Used the CommonLabels approach, but changed:
Code:
ctl.OnClick = "=HandleClick('" & ctl.Name & "')"
to
Code:
ctl.OnClick = "=HandleClick(" & ctl & ")"

so I was actually passing the control around.

Thanks for your help vbaInet and ChrisO, much appreciated.
 

Users who are viewing this thread

Top Bottom