How Can I Test to See If a Form is Open?

J

JonOfAllTrades

Good afternoon.
I often need to update the contents of other forms. If a form is not
visible, I would like to skip it, to avoid unnecessarily opening the form.
I've tried using "If Form_Name.Visible," but it is not always correct - I've
had it report false positives and negatives.
What's a better way to see if a form has been opened?
Thank you.
 
L

Larry Linson

The Forms collection only contains Open Forms. The following code
"enumerates" the Forms in the Forms Collection.

Function ShowForms() As Integer
Dim frm As Form
Dim intNoOpen As Integer
For Each frm In Forms
Debug.Print frm.Name
intNoOpen = intNoOpen + 1
Next
ShowForms = intNoOpen
End Function

Just one note of caution, a Form that is displayed in a Subform Control is
not Open -- its instance exists only as the Form property of the Subform
Control.

I'm hard pressed to think of a valid _requirement_ for one Form to update
information on multiple other Forms. If you have updated an underlying
table, you might want to Refresh or Requery the pertinent ones. You might
try that and just field the errors in case they were not Open.

In general, it is not good practice to have that kind of close "linkage"
between objects. It is best to make them each as self-sufficient as
possible. Sooner or later, the day will come that you make a change and do
not realize what related things need to be changed.

Larry Linson
Microsoft Access MVP
 
J

JonOfAllTrades

I agree, and the communication between forms is very limited. Each form
only has a few public functions. Typically, there are one or more functions
that just set flags indicating that the form needs to requery data. For
example, if the user renames a project, the code calls the global function
RequeryProjects, which runs Form_Contracts.RequeryProjects,
Form_Manpower.RequeryProjects, etc. Of course, I only want to run this
function on forms that are already open, thus my post. Ms Farnham's
suggestion has helped and I have it now.
The only other public functions I have on my forms are used to select a
new record. For example, if a user double-clicks on an entry in a list of
projects, I want to either open the Projects form to that project, or, if the
form is already open, I want to tell it to display this new project.
I whole-heartedly agree with the gist of your caution. I do my best to
keep data in black boxes, strictly limit public or global functions and
variable, channel transactions through intelligent functions, etc.
 
Top