Results 1 to 12 of 12
  1. #1
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Which Label did I click? (Access 2003)

    I thought that <post#=588,235>post 588,235</post#> was going to answert this, but it does not.

    I often provide continuous forms where the column headings act as sort buttons. They are labels with an on click event. I use the tag property of each to specify the field name to sort by.

    Private Sub VSurname_Label_Click()
    fnSort (VSurname_Label.Tag)
    End Sub

    I have thought I should be able to find some way of knowing which label has been clicked, so that the event could be the same for each control. Something like the following, but neither of these work.

    fnSort (screen.previouscontrol.Tag)
    fnSort (screen.activecontrol.Tag)
    Regards
    John



  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Which Label did I click? (Access 2003)

    ActiveControl and PreviousControl only refer to controls that have (or have had) the focus. Although labels do have an On Click event, they can't receive the focus, so they are never the ActiveControl or PreviousControl. Offhand I can't think of a clever way to do what you want.

  3. #3
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Which Label did I click? (Access 2003)

    Thanks Hans

    I had (more or less) given up on finding a solution, but was prompted to try again by seeing the post about combo boxes.
    If I changed the labels to command buttons then I could do it, but I think that would create more work than it would eliminate.
    Regards
    John



  4. #4
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Which Label did I click? (Access 2003)

    I've done the same thing. I just establish a form-level variable in code, and in the on-click event (after doing everything else), I move the label name to that field. This way I always know which is the "current" sort column.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  5. #5
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Which Label did I click? (Access 2003)

    What you say makes sense, but that is not quite my question.


    My concern is not with knowing the current sort order. When I click a label, I want the system to work out which label I clicked.

    At the moment I have:
    Private Sub VSurname_Label_Click()
    fnSort (VSurname_Label.Tag)
    End Sub

    Private Sub VFirstname_Label_Click()
    fnSort (VFirstname_Label.Tag)
    End Sub

    I write the name of the clicked label inside the procedure. If I had some function that could determine which one was clicked ( let's call it fnClickedLabel() as label ) I could use this.

    Private Sub VSurname_Label_Click()
    fnSort (fnClickedLabel().Tag)
    End Sub

    Private Sub VFirstname_Label_Click()
    fnSort (fnClickedLabel().Tag)
    End Sub
    Regards
    John



  6. #6
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Which Label did I click? (Access 2003)

    There is no easy way; that is, nothing like being able to use the ActiveControl property or something like that. In the Click event for each label, you will need to pass the labelname to a function, something like:
    =SomeFunction("Label_OrderDate")
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Which Label did I click? (Access 2003)

    John,

    What I usually do is use a custom label class to house the sort code. Here's the working part that handles the click event for labels used as column headers. It checks for a custom LabelClicked property on the form, which is set to the name of the label in its click event. The label's tag stores the name of the field to sort on when that label is clicked. Hope this helps.

    <pre>Private Sub mlbl_Click()
    'handles the click event of a label
    ' Created by Charlotte Foust
    On Error Resume Next
    Dim strTag As String 'holds the label's tag property value
    Dim strCurrentLbl As String 'holds the name of this label
    Dim frm As Form 'holds the label's parent form
    Dim blnDesc As Boolean 'flag for descending sort
    Static varDirection As Variant 'holds direction string for sort

    'assign the label's tag to a variable
    strTag = mlbl.Tag

    'if the tag is a string and not 'DETACHED LABEL'
    If strTag <> "" And strTag <> "DETACHED LABEL" Then

    'set the descending sort flag if DESC is in the tag
    blnDesc = (InStr(strTag, " DESC") > 0)

    'if a descending sort is flagged
    If blnDesc Then
    'store the field portion of the tag to the variable
    'note: this doesn't work for multiple field names in tag
    strTag = Left$(strTag, InStr(strTag, " DESC"))
    End If 'blnDesc Then

    'if the direction string is empty or not initialized
    If IsNull(varDirection) Or IsEmpty(varDirection) Then

    If blnDesc Then
    'if the descending flag is set store DESC
    varDirection = "DESC"

    Else 'blnDesc Then
    'otherwise store an empty string
    varDirection = ""
    End If 'blnDesc Then

    ElseIf varDirection = "" Then
    'if the direction string is empty, store DESC
    'to reverse the sort direction
    varDirection = "DESC"

    Else
    'if the direction string is DESC, store ""
    'to reverse the sort direction
    varDirection = ""
    End If 'IsNull(varDirection) Or IsEmpty(varDirection)

    'set the form variable to the label's parent form
    Set frm = Forms(mstrLblParent)

    'get the last label clicked
    strCurrentLbl = frm.LabelClicked

    'If this label is not the same one
    If strCurrentLbl <> mlbl.Name Then

    'store the name of the current label in LabelClicked
    frm.LabelClicked = mlbl.Name

    End If 'strCurrentLbl <> mlbl.Name

    'handle the click based on whether the
    'form is bound or unbound
    Select Case frm.RecordSource

    'Unbound form
    Case "", vbNullString
    'open the form referenced in the tag
    DoCmd.OpenForm strTag

    'Bound form
    Case Else
    'sort the data on the field name in the tag
    frm.OrderBy = strTag & " " & varDirection
    frm.OrderByOn = True
    End Select 'Case frm.RecordSource
    End If
    Set frm = Nothing
    End Sub 'mlbl_Click()</pre>

    Charlotte

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Which Label did I click? (Access 2003)

    It's a routine from a class, Giorgio. I posted the click event to give John an idea of how to handle his problem. I've attached the entire class as a text file. To use it, the form containing the labels has to declare a module level LabelClicked variable, plus object variable for each label, and bind the variable to the class by code something like this:

    Dim mlbl1 As clsLabel
    Dim mlbl2 As clsLabel
    Public LabelClicked As String

    in Form_Open, you would bind the controls with code to initialize each object variable:

    mlbl1 = Init(Me.Label1)
    mlbl2 = Init(Me.Label2)

    In the Clicked event of each label instance, the class will handle assigning LabelClicked.
    Charlotte

  9. #9
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Label did I click? (Access 2003)

    Thank you Charlotte,
    I can see that if the form is unbound, strTag only stores the form name otherwise it stores the field name and optionally the sort order, very clever. <img src=/S/rose.gif border=0 alt=rose width=15 height=15>

  10. #10
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Label did I click? (Access 2003)

    Hi Charlotte,
    Does mlbl.tag in your Private Sub mlbl_Click() store field, sort order and form info?
    Also, where do you set the mstrLblParent variable?
    And could you please post the code that sets up the LabelClicked property?

  11. #11
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Which Label did I click? (Access 2003)

    Thanks for this.
    Custom classes are not something I am very familiar with so I will to look at this very carefully to work out how it all works.
    Regards
    John



  12. #12
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Which Label did I click? (Access 2003)

    You could incorporate some of the functionality directly into your form. I merely created the class years ago so that I didn't have to program each form for that kind of behavior. Good luck with it.
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •