473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Multiple click events on labels

Hello all,

I am using the below code as a click event for a label. It works fine but the problem is I have multiple labels which require the same event to occur on click. Please find the code below:

Expand|Select|Wrap|Line Numbers
  1. Private Sub label1_click()
  2.     If Label1.Caption = Chr(254) Then
  3.         Label1.Caption = Chr(168)
  4.     Else
  5.         Label1.Caption = Chr(254)
  6.     End If
  7. End Sub
  8.  
  9. Private Sub label2_click()
  10.     If Label2.Caption = Chr(254) Then
  11.         Label2.Caption = Chr(168)
  12.     Else
  13.         Label2.Caption = Chr(254)
  14.     End If
  15. End Sub
Nov 23 '13 #1

✓ answered by ADezii

This is a rather odd request, but I'll Post an alternative, then rely on your explanation as to why.
  1. Create a Private Function in the Form's Class Module that will process each Label as passed to it in it's Click() Event, and act accordingly:
    Expand|Select|Wrap|Line Numbers
    1. Private Function fEvalLabel(lbl As Access.Label)
    2. If lbl.Caption = Chr(254) Then
    3.   lbl.Caption = Chr(168)
    4. Else
    5.   lbl.Caption = Chr(254)
    6. End If
    7. End Function
    8.  
  2. Call the Function from the Click() Event of each Label, passing to it the Label Control itself:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label1_Click()
    2.   Call fEvalLabel(Me![Label1])
    3. End Sub
    4.  
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label2_Click()
    2.   Call fEvalLabel(Me![Label2])
    3. End Sub
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label3_Click()
    2.   Call fEvalLabel(Me![Label3])
    3. End Sub

5 5322
ADezii
8,834 Expert 8TB
This is a rather odd request, but I'll Post an alternative, then rely on your explanation as to why.
  1. Create a Private Function in the Form's Class Module that will process each Label as passed to it in it's Click() Event, and act accordingly:
    Expand|Select|Wrap|Line Numbers
    1. Private Function fEvalLabel(lbl As Access.Label)
    2. If lbl.Caption = Chr(254) Then
    3.   lbl.Caption = Chr(168)
    4. Else
    5.   lbl.Caption = Chr(254)
    6. End If
    7. End Function
    8.  
  2. Call the Function from the Click() Event of each Label, passing to it the Label Control itself:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label1_Click()
    2.   Call fEvalLabel(Me![Label1])
    3. End Sub
    4.  
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label2_Click()
    2.   Call fEvalLabel(Me![Label2])
    3. End Sub
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Label3_Click()
    2.   Call fEvalLabel(Me![Label3])
    3. End Sub
Nov 24 '13 #2
Hi Adezii,

Thanks for the reply. I have about 200 labels on my spreadsheet that need a loop instead of writing a code for each label. I am using wingding character on the label property to create a larger looking checkbox...

I have the same code running for multiple labels but instead would like to run a code that loops all the labels to the same code. eg; label 1,2....200. Any suggestion is appreciated..

Regards
Frank...
Nov 24 '13 #3
Seth Schrock
2,965 Expert 2GB
If all of your labels need the same code performed on them, then you can loop through all the labels. However, if there are any labels used that you don't want to test, then you can use the Tag property of the label to specify that the loop should test that label. However, if you want only want the code ran when you click on it, then you will have to write code in each label's OnClick event. Otherwise, clicking the label won't trigger anything to happen, so I really don't see a benefit to looping through all the labels. In fact, I'm betting that ADezii's solution would perform faster since you are only dealing with one control at a time and not having to loop through the controls that aren't labels as well.

If you still want to loop through the controls, then the code to do so would be something like this.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl as Control
  2.  
  3. For Each ctl in Me.Controls
  4.     If ctl.ControlType = acLabel Then
  5.         With ctl
  6.             If .Properties("Caption") = Chr(254) Then
  7.                 .Properties("Caption") = Chr(168)
  8.             Else
  9.                 .Properties("Caption") = Chr(254)
  10.             End If
  11.         End With
  12.     End if
  13. Next ctl
Nov 24 '13 #4
NeoPa
32,556 Expert Mod 16PB
I have the same code running for multiple labels but instead would like to run a code that loops all the labels to the same code. eg; label 1,2....200. Any suggestion is appreciated.
You seem to be missing the basic point that Object-Oriented Programming (OOP) is not procedural.

Event handlers are an element of OOP. They don't always run together. They are simply available to be invoked by the system when the specific events occur. The way the system is notified that you may want code to run for any event is that you create an event handler for it. This is true for each and every such event. There is no option to specify any list of controls as requiring event handlers.

ADezii has already provided the best answer you can get for this question.

NB. Your latest post says you are working in a spreadsheet. I must assume that is a typo as this is clearly an Access/VBA forum. The Excel forum is now separate.
In Excel the answer would be somewhat different I suspect, as the events available are different.
Nov 24 '13 #5
Thanks Guys!!! Adezii's code is good to go.
Nov 24 '13 #6

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

Similar topics

1
by: zoltix | last post by:
Hi, I am beginner in JavaScript. I would like to intercept all click events on my document. I use this function for that document.onmousedown=click;. It works well. But I would like to...
2
by: DS | last post by:
Any known problems or downsides to using Click Events to fire off Macros on Labels instead of using Command Buttons. The Labels give me much more flexability as far as the design goes instead of...
0
by: Anina | last post by:
It looks like the button control captures all of the clicks on the button no matter if the program is busy accessing code. For example: if the click event of a button has code that takes 10 seconds...
0
by: joe | last post by:
Hi, I have an aspx page that reads a session variable and loads an appropriate set of user controls. It appears that the user is going to different pages when in reality it is the same page with...
24
by: ej1002 | last post by:
Hi I have developed a Windows Application(C# Windows Form) which will get the IFrame Source of the page it is navigating using Webbrowser Control. Now I want do this in ASP.Net web application(C#...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
15
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
5
by: Amoril | last post by:
I've read quite a few different message on various boards and for some reason I'm still having trouble wrapping my head around this viewstate maintenance and trying to get these dynamically created...
6
by: Michael Landberg | last post by:
Hi is it possible to load multiple onload events in the body tag? Regards
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.