Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Screen.ActiveControl.Name


    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?

  2. #2
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    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

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    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.

  4. #4
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    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

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    If the label is attached, it is a member (the only one) of the control's zero based Controls collection.
    is there anyway to get the name of a label control when one clicks on a label?
    What labels do you have that have any events?? I've only ever seen that on one type and everything else, no events.

    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.

  6. #6
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by GraeagleBill View Post
    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?
    Yes it is. Using a class module.

    Code of a class module named clsLabel:
    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 of your form:
    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.

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    What labels do you have that have any events?
    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.

    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:

    Click image for larger version. 

Name:	000.jpg 
Views:	25 
Size:	41.6 KB 
ID:	46424

    Click image for larger version. 

Name:	001.jpg 
Views:	25 
Size:	21.1 KB 
ID:	46425

    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

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    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.

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Well, I asked if the labels were attached
    Oops! Sorry, I only answered one of your two questions. No, the labels are not attached.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    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.

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    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

    Click image for larger version. 

Name:	002.jpg 
Views:	24 
Size:	28.8 KB 
ID:	46433

    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"

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    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.

  13. #13
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    And the search goes on!
    Thanks,
    Bill

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    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

    Code:
    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
    However, that won't work at all for this purpose

    2. Access365 does supply a method of getting the name of any label attached to another control

    Click image for larger version. 

Name:	Capture.PNG 
Views:	21 
Size:	5.6 KB 
ID:	46440

    Example code to get the attached label name & caption
    Code:
    Debug.Print Me.txtPurpose.Controls.Item(0).Name, Me.txtPurpose.Controls.Item(0).Caption
    Again that is of no use for an unattached label

    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
    Attached Files Attached Files
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    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
    Code:
    Me.Last_Name_Label.OnClick = "=GetCtlName(""" & Me.Last_Name.Name & """)"
    - as long as you provide the name, which sort of defeats the goal.
    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
    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)
    which is pretty much useless when I had to provide the proc name in the first place.

    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.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. DoCmd.GoToRecord , , acGoTo, Screen.ActiveControl
    By hnhpak in forum Programming
    Replies: 6
    Last Post: 04-14-2015, 07:13 AM
  2. ="[Mem_ID] = " & Str(Nz(Screen.ActiveControl,0))
    By Mohammadsharif in forum Access
    Replies: 2
    Last Post: 04-09-2015, 06:30 AM
  3. Replies: 9
    Last Post: 09-16-2014, 03:56 PM
  4. Usage of Screen.Activeform.ActiveControl - syntax
    By dcdimon in forum Programming
    Replies: 6
    Last Post: 06-17-2014, 09:05 AM
  5. Help with gotocontrol, activecontrol
    By k9drh in forum Forms
    Replies: 5
    Last Post: 04-21-2011, 10:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums