0

When I click on a button that creates a popup form, my VBA code Screen.ActiveForm still references the form containing the popup button, even when the popup form has focus. How do I reference the popup form in this case? So confused why Access doesn't register the popup with focus as the active form...

Mike
  • 961
  • 6
  • 19
  • 43

2 Answers2

4

You can use the Forms collection which lists all open forms

Forms!MyPopup

or, if you have invalid characters in the name of the form

Forms![My Popup]

or

Forms("My Popup")

You can access controls on it with

Forms![My Popup]!TextBox1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • This works... but I am also trying to get the name of the active control without having to type its literal name. Forms("my popup").ActiveControl.Name gives `Run-time error 2474: The expression you entered requires the control to be in the active window` as soon as the popup appears (I have the code in the `Form_BeforeUpdate` event. – Mike Jan 22 '16 at 17:25
  • Set the focus to the form with `Forms![My Popup].SetFocus` before you get the active control. – Olivier Jacot-Descombes Jan 22 '16 at 17:42
  • I get the same error with the code `Forms("TaskBasic").SetFocus` newline `Debug.Print Forms("TaskBasic").ActiveControl.Name` – Mike Jan 22 '16 at 17:54
  • New info: I get that same error message, but it does actually output the name of the correct control... should I simply ignore the error message or it is possibly going to bite me in the butt later? – Mike Jan 22 '16 at 18:09
  • You might have an issue with the `Form_BeforeUpdate` event handler still running. You could go the other way round and put the code in the popup form into the control AfterUpdate and send the value to the main form: `Forms!MainForm!TextBox1.Value = Me!TextBoxInThePopUp.Value` – Olivier Jacot-Descombes Jan 22 '16 at 18:09
  • I can't do that because I need a dynamic reference to the active control since there are so many. But thanks for the help. The same error comes up in the `AfterUpdate` event. Not sure why Access makes it impossible to reference the active control in an active popup... thanks for your help. – Mike Jan 22 '16 at 18:22
  • I'm not sure why you are getting this error. I made a test and don't get it - even when the popup isn't active. Are you sure that this is the statement causing the error? – Olivier Jacot-Descombes Jan 23 '16 at 14:26
  • Hmm odd... yes the `debug.print` is the highlighted statement when I go to the debugger. – Mike Jan 25 '16 at 04:35
  • `ActiveForm` and `ActiveControl` do not work from the Immediate window, because the Immediate window takes away the focus from Access Forms and Controls! – Olivier Jacot-Descombes Jan 25 '16 at 13:51
0

when opening/creating the pop-up form, store the form name in a variable, then reference that variable when required.

dim sPopup$

sPopup="FRM_Popup"

docmd.openform sPopup

...reference popup form here using sPopup...
RyanL
  • 1,246
  • 2
  • 10
  • 14