As it is, Screen.ActiveControl.Name pertains to the control that has the focus. Since label controls don't get the focus, is there anyway to get the name of a label control when one clicks on a label?
As it is, Screen.ActiveControl.Name pertains to the control that has the focus. Since label controls don't get the focus, is there anyway to get the name of a label control when one clicks on a label?
You could set a global variable on the label click . Then use the variable for whatever you need it for.
If this helped, please click the star * at the bottom left and add to my reputation- Thanks
How would that get its name. In the current case, the label click event references a function with arguments totally un-related to the name of the label control. I could compound the argument to include the name of the control, but I'd like to avoid the effort if I can.
What is it that your doing? I take it you have a lot of labels and your using "=SomeFunction()" in the On Click event and screen.activecontrol in the function.
Could you use a textbox with no border, not a tab stop, and locked? It looks like a label.
After getting the control name I also used screen.activeform to shift focus from the textbox to another control which helps to mimmick a label (no cursor).
If this helped, please click the star * at the bottom left and add to my reputation- Thanks
If the label is attached, it is a member (the only one) of the control's zero based Controls collection.
What labels do you have that have any events?? I've only ever seen that on one type and everything else, no events.is there anyway to get the name of a label control when one clicks on a label?
EDIT - "the control's" means the control the label is attached to, such as a textbox. Not sure what you can get from that, but .Name and .Caption ought to be at least 2 of them.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Yes it is. Using a class module.
Code of a class module named clsLabel:
Code of your form:Code:Option Compare Database Option Explicit Public WithEvents Target As Access.Label Private Sub Target_Click() 'You may code this proc as you need. MsgBox Me.Target.Caption, , Me.Target.Name End Sub
Code:Option Compare Database Option Explicit Private mcolLabels As Collection Private Sub Form_Close() Set mcolLabels = Nothing End Sub Private Sub Form_Load() Dim L As clsLabel Dim C As Control 'Keep an instance of the clsLabel for each label in the form. Set mcolLabels = New Collection For Each C In Me.Controls If TypeName(C) = "Label" Then C.OnClick = "[Event Procedure]" C.ControlTipText = "Click on " & C.Name Set L = New clsLabel Set L.Target = C mcolLabels.Add L, C.Name End If Next C End Sub
Last edited by accesstos; 10-18-2021 at 05:25 PM.
In the past, about the only time I've ever used label events was when the label headed a column on a form. The event "code" would trigger sorting of the RecordSource of a column oriented form and change the color of the label, blue ascending, red descending. The same code would change the font weight to bold and tweak the "Top" and "Left" properties of the label to compensate for the increased size of the label resulting from "bold". In concert, variables global to the form keep track of the RecordSouurce field name to set the Orderby arguments.What labels do you have that have any events?
I've also used column label events in conjunction with the Ctrl key to open a search pane above the label in the header section if the user is trying to find a particular piece of data.
Sort of a long answer to your question, but thought it might be of some value to other interested forum users.
Excerpts from one of my app's HELP file:
Regarding the OP, I do have alternative methods available in the current app to accomplish the task without having to deal with a label event, so I'll close the thread now.
Thanks everyone,
Bill
Well, I asked if the labels were attached but...
Could be as simple as a Mouse event, but I find it curious that users are enticed to click on labels in the first place. Might as well be a textbox that looks like a label instead.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Oops! Sorry, I only answered one of your two questions. No, the labels are not attached.Well, I asked if the labels were attached
I have no idea on how to know which label was clicked on even though it will have an event, but I bet there is a way. If you find code could you post a link?
I can only think of getting the name of the event up to the 2nd to last underscore but I imagine there are other ways.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
I'm not sure this answers your question, but here's the typical event property and code that corresponds to the screenshots I posted in #7
Code:Public Function CommonSortRtn(SortBy As String) '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* ' The column heading labels have invocations of this routine in their "on click" ' events to set the sort attributes of the display to sort on the field corresponding ' to the column. ' ' IMPORTANT: The input "SortBy" parameter is the name of the table field. The name ' of the label control that heads that particular column is derived as ' "ColLbl" & SortBy. It is then those controls that have their highlighting ' toggled as the user moves from one column to another. '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* On Error GoTo SortError 'Clicking on an already selected column" If SortField = "tb" & SortBy Then If Me.OrderBy = SortBy & " DESC" Then Me.OrderBy = SortBy Else Me.OrderBy = SortBy & " DESC" End If Else Me.OrderBy = SortBy End If Me.OrderByOn = True Me.Requery Un_Highlight (SortColumn) 'Un-highlight current heading SortColumn = "lbl" & SortBy 'Label control name for the column heading Highlight (SortColumn) 'Highlight the new selection SortField = "tb" & SortBy ' we need the member name of the controls collection for "alpha" searches. NominalExit: Exit Function SortError: MsgBox "Error: " & Err.Number & " encountered in Common Sort function." & _ "Description: " & Err.Description & ". (Ignoring error and attempting to resume.)" GoTo NominalExit End Function
In the Open Event of the form: (Variables Dim'd global to the form)
Code:SortColumn = "lblLastName" 'This will change per user selects Highlight (SortColumn) 'Highlight column label as currently sorted SortField = "tbLastName" Me.OrderBy = "LastName, ASC"
No, I've done that before, but thanks anyway. In that case, by providing a parameter to the function you're passing the control name. I was referring to a procedure that would pass the name by simply referring to some property of the event. What that would permit is knowing not just the event (click, double click, etc.) but the name of the control that triggered it. To use non-code words:
Private Sub myLabel_Click()
msgbox EventProperty.ClickedControl.Name
End Sub
which would display a message box containing 'myLabel'. I figure that the click event (or some other event) must have a property or method whereby it knows which control was clicked on besides what is showing in the event name. I'll look into WithEvents to see if there's something there. All I can think of at the moment is something like
InstrRev(Event Name Here, "_") & get the position of the last _ (21 in above event?)
InstrRev(Event Name Here, " " & get position of space before name (13?)
Do the math to get Mid (13,7)
The problem with that theory is how do you get the event name??
If I come up with anything, I'll post it here if you care to know.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
And the search goes on!
Thanks,
Bill
I realise this has now been marked solved but I started this reply several hours ago & forgot to click Send....
1. For info, Access doesn't provide any simple method of getting the name of the current procedure using code.
It can be done as part of error handling reasonably successfully using the Microsoft Visual Basic for Applications Extensibility 5.3 library
However, that won't work at all for this purposeCode:Private Sub YourProcedureNameHere() On Error GoTo Err_Handler 'code here Exit_Handler: Exit Sub Err_Handler: strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0) MsgBox "Error " & Err.number & " in " & strProc & " procedure : " & Err.Description Resume Exit_Handler End Sub
2. Access365 does supply a method of getting the name of any label attached to another control
Example code to get the attached label name & caption
Again that is of no use for an unattached labelCode:Debug.Print Me.txtPurpose.Controls.Item(0).Name, Me.txtPurpose.Controls.Item(0).Caption
3. However, attached is a simple example database showing 3 methods of getting an unattached label name & caption using code.
It has two forms
Form1 - Shows how the name & caption can be displayed both using a mouse move event & a click event
Form2 - Demonstrates the class module code supplied by @accesstos in post #6 but which seems to have been overlooked until now
Hope that is of some use
I tried that (after it was edited?) and have not yet figured out why 2 msgboxes are presented, one with a caption of "Microsoft Access" and the other with "Last Name_Label".
Also tried a bunch of other things and learned some stuff that I'll probably soon forget, but everything I came up with required that you know the property value of something such as name, tag or the procedure line. One neat (?) trick was to use MouseDown to set the OnClick event, which fires afterwards. So you can pass the control name to the click event property with
- as long as you provide the name, which sort of defeats the goal.Code:Me.Last_Name_Label.OnClick = "=GetCtlName(""" & Me.Last_Name.Name & """)"
Surely we've seen how to call the same one function for a bunch of controls and pass the control name in the event property, but again, the parameter (control name) has to be entered in every control's function call. Again, not ideal. Same thing if you use the tag property.
Lastly there was the vbe part. I can get the line number of a procedure
which is pretty much useless when I had to provide the proc name in the first place.Code:lngLine = Application.VBE.ActiveCodePane.CodeModule.ProcBodyLine("testDLwithLike", 0) then pass the line number to another property to get the procedure name MsgBox Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(16, 0)
As for .Controls(0), I mentioned that around post 5.
So maybe accesstos will explain the double msgbox. Gonna park this for tonight, I think.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.