Solved Cannot Assign ActiveControl/ Screen.ActiveControl (1 Viewer)

dalski

Member
Local time
Today, 23:58
Joined
Jan 5, 2025
Messages
208
I have a PopUp Form containing two listboxes (the control I refer to is the child listbox). Parent listbox has no multi-select property; child listbox (in question) mutli-select (simple) & an optionGroup (irrelevant) with other irrelevant buttons...
  1. MouseDown event assigned to a txtBox in the child listbox
    1. Correctly reads Me.ActiveControl.Name as the child listbox.
    2. Screen.ActiveControl.Name = (Error 2474 - control to be in active window). Google suggests Screen.ActiveControl will always return the control which had the focus when the event was activated). This does not seem to be the case & I mention multiple listboxes incase this is the issue; though I think it should not be.
  2. Since 1.1 can access the control name correctly it should be able to assign an obj var of that control (child listbox) either with:
    1. Code:
      Set ctl = Me.ActiveControl
      ; or
    2. Set a string var based on name of ActiveControl & use that to reference the Controls collection. EDIT - this works but the others should also work & want to learn WHY?
    3. Let alone should be able to reference Screen.ActiveControl.
Why is the Screen.ActiveControl not working & why can't I assign the ctrl to an object variable of control type?

I imagine an explicit ListBox obj var type would eradicate the issue but I cannot see why i cannot pass a generic control then ascertain what that control type is in an external procedure; allowing for simpler processing of the control type. Reference the childListbox control; unless an explicit reference is used:
Code:
Set ctl = Controls("childListBoxName")

Which is not suitable as I need to assign the controls at run-time. I can pass the string reference of the control name to another procedure & that's fine but it would be much better to pass the actual control itself; or better not have to pass the control & use the Screen.ActiveControl property in the called procedure.

Any ideas why this is happening?
 
Last edited:
Interestingly specifying an objVar as Listbox type does not assign the listbox objectively (without an explicit string reference) either:
Code:
Set thisList = Me.ActiveControl
 
Firstly, I would advise against using Screen.ActiveControl as focus is often not where you expect it to be.

Eg click a button the tries to get the value of the control that was previously focused and Screen.ActiveControl will now be the button.

Also, if you are stepping through code in the VBA editor, then the VBA editor will have the focus!

If you must pursue, you can add code like this before each time you need to use the Screen.ActiveControl to see what's really what:
Code:
With Screen
  Debug.Print "ActiveControl:", .ActiveControl.Name, "PreviousControl:", .PreviousControl.Name
End With
Then you can see what is actually being seen/used in the Immediate Window (Ctrl+G)
 
Thanks Dave, that's exactly what's happening & pretty much what I'm up to.

But the unreliability of Screen.ActiveControl also seems to apply to Me.ActiveControl.

PreviousControl also produces an inaccurate result; specifying a button which i have not pressed in years. Interestingly Screen.Previous.Control errors out in the called procedure; which isn't an issue as I simply will not use it, but I mention for clarity.

I removed breakpoints & stopped stepping through the code as usual you're right. The problem seems to be when stepping through the code the focus is lost from the ActiveControl. If I put a debug.print in the called procedure the ActiveControl is accurate in the called procedure. So future users beware - as @cheekybuddha astutely pointed out; debugging your code removes focus from controls & will provide different results from code that it is ran uninterruped (as an app will be).
 
The issue is simple - you are using popup forms.

Popup forms cannot be directly identified as active using Screen.ActiveForm. This is because they are located 'on top ' of the screen rather than being part of it

1760268970584.png


Similarly, the selected control in a popup form cannot be identified using Screen.ActiveControl

1760269013269.png


For more detail, see my article:

Unfortunately, there are no equivalent items ActivePopupForm / ActivePopupControl
 
Thanks @isladogs, I had no idea & I'm glad I mentioned PopUp (which I thought would be irrelevant). Some valuable lessons learned here; thanks guys.

I should credit & boost page rank for the awesome tutorial I am following - Doug Steele's awesome Drag & Drop tutorial here; which I think is @xavier.batlle? If so thank you, I'm really enjoying it & you're most generous in sharing such an awesome technique. To state the obvious the trouble I encountered is completely self-inflicted & in no way diminishes the value of the tutorial.
 
Last edited:
Thanks @isladogs, I had no idea & I'm glad I mentioned PopUp (which I thought would be irrelevant). Some valuable lessons learned here; thanks guys.

I should credit & boost page rank for the awesome tutorial I am following - Doug Steele's awesome Drag & Drop tutorial here; which I think is @xavier.batlle? If so thank you, I'm really enjoying it & you're most generous in sharing such an awesome technique. To state the obvious the trouble I encountered is completely self-inflicted & in no way diminishes the value of the tutorial.
Hi @dalski, this tutorial is by Doug Steele. No credits for me, but thanks.😉
 
Thanks xavier.battle, apoligies Doug Steele. If Doug is a member would greatly appreciate if someone could tag him so he gets credit deserved.
 
AFAIAA, former MVP, Doug Steele isn't a member here but I agree its an excellent tutorial
 

Users who are viewing this thread

Back
Top Bottom