Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Aug 2001
    Location
    Toronto, Ontario, Canada
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Screen.ActiveControl (97)

    Hi All. Just wondering if anyone here can help with this.

    I have a form which has a couple of multi-select list boxes. With the help of someone who knows far more than me, I was able to get the following code to work.

    What I am trying to do now is to use the Screen.ActiveControl command to be able to distinguish from which list box the user has made a selection and fire that criteria over to the appropriate query. I would use an if statement at the Set QueryDefs depending on the name of the ActiveControl.

    *********** start of working code ************************************************** **************************
    Dim Q As QueryDef
    Dim db As Database
    Dim Criteria As Variant
    Dim ctl As Control
    Dim Itm As Variant

    Set ctl = Me![SelectCategory]

    For Each Itm In ctl.ItemsSelected
    If Len(Criteria) = 0 Then
    Criteria = "'" & ctl.ItemData(Itm) & "'"
    Else
    Criteria = Criteria & ",'" & ctl.ItemData(Itm) & "'"
    End If
    Next Itm

    If Len(Criteria) = 0 Then
    Itm = MsgBox("You must select one or more items in the list box!", 0, "No Selection Made")
    Exit Sub
    End If

    Set db = CurrentDb()

    Set Q = db.QueryDefs("qryCategorySelection")
    Q.SQL = "Select * From [qryCatalog] Where [CategoryName] In(" & Criteria & ");"
    Q.Close
    *********** end of working code ************************************************** ***************************

    This does not work, however :
    Set ctl = Screen.ActiveControl
    For Each Itm In ctl.ItemsSelected

    I also tried
    For Each Itm in Screen.ActiveControl.ItemsSelected

    It works fine when referencing one specific control, but goes wobbly when I try to expand on that and I get the error message Number 2455 , You entered an expression that has an invalid reference to the property ITEMSSELECTED. The property may not exist or may not apply to the object you specified

    It goes all goofy at the ItemsSelected point.

    What is odd though, is that when I type it in the drop down box offers the selection of ItemsSelected after ActiveControl. However, when I type a full stop after ItemsSelected the choices Count or Item pop up??? Tried adding .Item at the end of it, but this time the error message was Argument Not Optional, so I assume I screwed that bit up.

    Please - I cannot believe that I have to set up separate code for each List Box on my form.

    Thanks in advance for any help!! Cheers,

    Donna

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

    Re: Screen.ActiveControl (97)

    One thought comes to mind immediately, are you absolutely sure that the ActiveControl at the time you are asking for it is indeed a listbox?

    Where are you running this code? That is, how is it initiated? I assume you are clicking on something, or is it in a listbox's AfterUpdate event, or what?
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

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

    Re: Screen.ActiveControl (97)

    I would suggest you make sure your controltype is acListBox before you try to reference its listbox properties. Otherwise, you'll get an error because only multiselect list and comboboxes have an itemsselected property. Intellisense isn't as intelligent as we might wish. Not every item it lists is valid for all instances.
    Charlotte

  4. #4
    Lounger
    Join Date
    Aug 2001
    Location
    Toronto, Ontario, Canada
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Screen.ActiveControl (97)

    Hi!

    Yeeessss - v. embarassed by this. Realised late yesterday afternoon that I was using a button to run the code, therefore the activecontrol was not the list box. Bit of a twit - learning curve just made a humungous turn for the better though.

    Thanks guys for taking the time to respond. I'll shuffle away to my dark corner.

    <img src=/S/smile.gif border=0 alt=smile width=15 height=15>)

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

    Re: Screen.ActiveControl (97)

    Use a label control instead of a button (set it's effect property to Raised), and you can use your code (in label's Click event).
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  6. #6
    Lounger
    Join Date
    Aug 2001
    Location
    Toronto, Ontario, Canada
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Screen.ActiveControl (97)

    Mark - Wow!!! Stunning. Was about to use screen.previouscontrol but this waaaayyyy better.

    Can you explain to me why it works using a label instead of a command button. During my "play time" I set it's click event procedure to be msgbox screen.activecontrol.name, and it always came up with the list box. when I did the same thing on a command button it gave the name of the command button (which as you well know was the cause of the problem last time).

    So how does it work?? Is it almost like the "previouscontrol" property? Or just that a label cannot be an active object. Would love to hear more. Thanks so much!!

    Donna

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

    Re: Screen.ActiveControl (97)

    You wrote:
    >>Can you explain to me why it works using a label instead of a command button. <<

    Because we make use of the knowledge that a label never gets the focus! That is, it is never the ActiveControl. So, whatever control has the focus when the Label is clicked is still the ActiveControl. The only flaw is if someone clicks on some other control first. However, you could check in your Label code to see that the current ActiveControl was one of the list boxes.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

Posting Permissions

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