Company logo


Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Versión en español de este artículo

Watch out for the ActiveForm trap

Some good advice to keep in mind when using the ActiveForm property.

You probably know that _SCREEN.ActiveForm contains a reference to whatever form is currently active in your application. You can take advantage of this when writing generic code. For example, if you want a generic way of closing the current form, you could do so as follows:


You might put this code in the procedure associated with the Close command on the File menu, for instance.

However, when no form is currently open, any reference to _SCREEN.ActiveForm will produce the error message: "ACTIVEFORM is not an object". So if the user clicks on your Close command when there was no form to close, the error will occur.

You can test for this condition by using the TYPE() function. Thus, TYPE("_SCREEN.ActiveForm") will return U (for undefined) if there is no open form. Alternatively, test _SCREEN.FormCount, which will contain zero when there are no open forms.

In the case of a menu command, a good place to perform this check would be in the menu item's Skip For clause (example: SKIP FOR _SCREEN.FormCount = 0). That way, the command will be greyed out, thus indicating that it currently has no effect. If you are using the Menu Builder, you can enter the Skip For condition in the Prompt Options window, which you reach by clicking on the button to the extreme right of the prompt.

Mike Lewis Consultants Ltd. November 1999.

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.