Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72

    Unanswered: Which Control did the User Click On?

    Say a user clicks on control on a form, what is the identifier that would indicate it's index in the collection?

    What I would like to do, is when the user clicks on the control, it would call a procedure and pass in it's index as an argument.

    This way I have one procedure that is called by all the controls.

    To create the controls, I:

    Set ctl = CreateControl(frm.Name, acLabel, acDetail, , , 0, 0, 1440, 1440)

    I can refer to the control by its index and change its properties based on a row in the recordset. :

    frm.Controls(ndx)

    Now the user want to click on it and get the detail.

    frm.Controls(WhatIsTheIndexOfTheControlTheUserClic ked?)

    There must a set ctl = ControlHavingFocus() or sumthin'

    How do I know which control they clicked on?

    Thanks,
    Carl

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hmm. I am sure I am being dim but I can't see an elegent way to do this. One way, however:

    Read the Me.ActiveControl.Name property. Enumerate the form's controls collection, loop through and as soon as the name matches the active conrols name you have the index (For i = 0 to Me.Controls.Count.... If Me.Controls(i).Name = Me.ActiveControl.Name.... Then i = the index of the active control).

    Actually - I've just reread your post. I don't think you need the index or even the name to do what you want to do - ActiveControl should be plenty.

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Mar 2004
    Location
    belgium
    Posts
    290
    the collection is not always in the same sequence, so that makes it a bit uneasy, but to find the index you can create an controls collection (public or private) of the form and check the control-name in a for next ( or for each control in .. ) by using the on click event, so if true you got the index.

    regards emiel

  4. #4
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    Youz both are onto something, the Me.ActiveControl.Name and the part about it being sketchy. Song and a Prayer if my names line up with my ordinal. I gen'd the form programatically, so it should be somewhat predictable as far as index in the collection goes.

    I wish there were somthing like a Me.ActiveControl.OrdinalPosition

    No?

    Thanks folks,
    Carl

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi Carl

    You can use the name instead of the index for most stuff. Or you can even use the ActiveControl object.
    e.g.:These are all equivelent for an active control called txtMyTextbox whose index is 8:
    Code:
    Dim Ctl As Access.Control
     
    Set Ctl = Me.Controls.Item(8)
     
    Set Ctl = Me.Controls.Item("txtMyTextbox")
     
    Set Ctl = Me.ActiveControl
    BTW - the loop I suggested loops through the controls by index - as such it is not prone to the error a control object iterant may be (except in the event that controls are created or deleted during processing of the loop).
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    Code:
    Private Sub Label0_Click()
    Dim ctl As Control
    
    Set ctl = Me.ActiveControl  'or Screen.ActiveControl
    MsgBox ctl.Name  'what I'd really like here is the index
    
    End Sub
    Results in Runtime error 2474 The expression you entered requires the control to be in the active window.

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Why do you want the index? What do you do with it?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  8. #8
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    That way I can tell what control the user clicked on. I can use this to seek the row in the recordset that was used to populate the control and display further details.

    Here's what I do:
    I have an ADO recordset that I display as a crosstab with undetermined/dynamic columns, I have a form with about 400 visible=false labels in it.
    I open an ADO recordset, and loop thru it, moving/displaying/setting the colors for the header row, etc, works great.

    When I created the forms, I set the .OnClick = "Macro1", Macro1 currently has a MsgBox that claims I can add functionality to the click
    Thanks,
    Carl

  9. #9
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Ah - just noticed you have been using a label all along. Nope - won't work - a label cannot be active. Only controls that can receive focus can be active.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  10. #10
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Presumably you then have to loop through your controls when populating from the recordset.

    Do you have a naming convention for your controls? If so - what is it? There might be another way....
    Testimonial:
    pootle flump
    ur codings are working excelent.

  11. #11
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    Labels, damn it, I'll try text boxes. When I iterate to generate the form (not render the form from the recordset), I just left the default names. Perhaps I can get a bit more clever.

    Thanks for all the tips, I'll keep you updated if I have luck or not.

    Carl

  12. #12
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    possibly a digression, but take a look at this

    ...although the goal is different, it identifies the clicked control (thanks Lansing!!) through any degree of subform nesting (thanks Bachatero!!)

    izy
    currently using SS 2008R2

  13. #13
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    I'm going with the .Tag property of the control. I will embed the parameters I need within the tag. I was just scanning the properties list for a textbox, and I thought, why not use the .Tag?

    Carl

  14. #14
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72

    Exclamation

    So how do I set the control's onClick property to call a procedure that takes the control's .Tag property as an arguement?

    Code:
    Dim ctl as Control
    Dim frm as Form
    
    With ctl
        .OnClick = "[procGetDetails]" & frm.ctl.tag  'This doesn't work
    End With
    In the help it says:
    To run this Use this syntax Example
    Macro "macroname" Button1.OnClick = "MyMacro"

    Event procedure "[Event Procedure]" Button1.OnClick = "[Event Procedure]"

    User-defined function "=functionname( )" Button1.OnClick = "=MyFunction()"

  15. #15
    Join Date
    Jul 2004
    Location
    Edmonton, Canada
    Posts
    72
    So I can get the
    .OnClick = "=procGetDetails(" & rs!IntegerVal & ")"

    But when I replace rs!IntVal with a rs!StringVal, I get a:

    "The object doesn't support the automation object "whatever the val of rs!StrVal"

    Why can the number get passed, but not a string?

    Thanks,
    Carl

Posting Permissions

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