How to refer the Activeform?

prabha_friend

Prabhakaran Karuppaih
Local time
Today, 06:44
Joined
Mar 22, 2009
Messages
1,044
Is it possible to know what is the ‘active form’ from a Module? Like we do in Word (Word.application.Activedocument) and Excel (Excel.application.Activeworkbook.Activesheet).
 
Well... you could use Screen.ActiveForm, but to be quite honest, it's very rare that I do this and it's quite fragile because if the code is running and another object get focus, the code could then fail. Generally, if you can, it's preferable to pass the form to the function.
 
You can use:

With CodeContextObject

and then referring to the Form as a [dot]

The advantage of this approach is that the Module is not Form specific and the code is portable from Form to Form.

Simon
 
Simon

That's awesome tip. I'm surprised I've not heard of this before. Thanks!

It works well even when called by form in a standard module and even if form calls another sub which then call the sub with CodeContextObject.

The only time it bomb, though, is if it's called by Immediate Windows.
 
Thank you for both Wazz and Banana. I am new to both screen object and the codecontextobject. Thank you again.
 
You have any idea why my intellisense is not listing the attributes of codecontextobject. It is very helpful if you know how to make it in the list. Because I am facing the same problem for somany objects. Thank you.
 
Well... you could use Screen.ActiveForm, but to be quite honest, it's very rare that I do this and it's quite fragile because if the code is running and another object get focus, the code could then fail. Generally, if you can, it's preferable to pass the form to the function.

I have a client who wanted a LARGE close button

I have a standard button with the click event =genericclose()

this function is coded

function Genericclose
with screen.activeform
docmd.close
end with
end function


all i need to do is copy the button to a different form.
 
Actually, thank Simon for the CodeContextObject.


The intellisense is working fine. It's just that CodeContextObject returns an 'Object', which, of course, is unknown until runtime. Be aware you still can use properties that are valid as if it was the actual object (e.g. Form, Report, whatever)

However, in that context, I'd limit myself to select few properties.. such as getting .Name for example as that is a property common to many objects (but not all!).
 

Users who are viewing this thread

Back
Top Bottom