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: - Private Sub label1_click()
-
If Label1.Caption = Chr(254) Then
-
Label1.Caption = Chr(168)
-
Else
-
Label1.Caption = Chr(254)
-
End If
-
End Sub
-
-
Private Sub label2_click()
-
If Label2.Caption = Chr(254) Then
-
Label2.Caption = Chr(168)
-
Else
-
Label2.Caption = Chr(254)
-
End If
-
End Sub
This is a rather odd request, but I'll Post an alternative, then rely on your explanation as to why. - 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:
-
Private Function fEvalLabel(lbl As Access.Label)
-
If lbl.Caption = Chr(254) Then
-
lbl.Caption = Chr(168)
-
Else
-
lbl.Caption = Chr(254)
-
End If
-
End Function
-
- Call the Function from the Click() Event of each Label, passing to it the Label Control itself:
- Private Sub Label1_Click()
-
Call fEvalLabel(Me![Label1])
-
End Sub
-
- Private Sub Label2_Click()
-
Call fEvalLabel(Me![Label2])
-
End Sub
- Private Sub Label3_Click()
-
Call fEvalLabel(Me![Label3])
-
End Sub
5 5322
This is a rather odd request, but I'll Post an alternative, then rely on your explanation as to why. - 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:
-
Private Function fEvalLabel(lbl As Access.Label)
-
If lbl.Caption = Chr(254) Then
-
lbl.Caption = Chr(168)
-
Else
-
lbl.Caption = Chr(254)
-
End If
-
End Function
-
- Call the Function from the Click() Event of each Label, passing to it the Label Control itself:
- Private Sub Label1_Click()
-
Call fEvalLabel(Me![Label1])
-
End Sub
-
- Private Sub Label2_Click()
-
Call fEvalLabel(Me![Label2])
-
End Sub
- Private Sub Label3_Click()
-
Call fEvalLabel(Me![Label3])
-
End Sub
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...
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. - Dim ctl as Control
-
-
For Each ctl in Me.Controls
-
If ctl.ControlType = acLabel Then
-
With ctl
-
If .Properties("Caption") = Chr(254) Then
-
.Properties("Caption") = Chr(168)
-
Else
-
.Properties("Caption") = Chr(254)
-
End If
-
End With
-
End if
-
Next ctl
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.
Thanks Guys!!! Adezii's code is good to go.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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#...
|
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.
...
|
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...
|
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...
|
by: Michael Landberg |
last post by:
Hi
is it possible to load multiple onload events in the body tag?
Regards
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
| |