How do I find out if a form is open?

F

Fred Wilson

I would like to find out if a form is open.

The idea is

If someform is open then close that form.

Execute code

Open closed form.

I suspect that I need to do

Dim strFormToClose as string, blItWasOpen as boolean
strFormToClose = "frmMainReportTool"
blItWasOpen = False

if strFormToClose is open then
forms(strFormToClose).close
blItWasOpen=true
End if

Execute code

if blItWasOpen then forms(strFormToClose).open

Thank you.

Fred
 
F

fredg

I would like to find out if a form is open.

The idea is

If someform is open then close that form.

Execute code

Open closed form.

I suspect that I need to do

Dim strFormToClose as string, blItWasOpen as boolean
strFormToClose = "frmMainReportTool"
blItWasOpen = False

if strFormToClose is open then
forms(strFormToClose).close
blItWasOpen=true
End if

Execute code

if blItWasOpen then forms(strFormToClose).open

Thank you.

Fred

Why would you wish to close a form and then open it again?
Simplest way is:
DoCmd.Close acForm, "FormName"
' No error is generated if the form is not already open.
DoCmd.openForm "FormName"

But.. to answer your question of how to know if a form is open:
What version of Access?
Access 2000 or newer?
To find if a form is open....
If Not CurrentProject.AllForms("FormA").IsLoaded Then
Do something here
Else
Do something else
End If

In Access 97, copy this function (from the Northwind.mdb sample
database) into a module:

Function IsLoaded(ByVal strFormName As String) As Integer
' Returns True if the specified form is open in Form view or
Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If
End Function

Then code some event:
If Not IsLoaded("FormA") Then
Do this
Else
Do that
End if
 
F

Fred Wilson

Well seems how you asked about the closing a form.

This form is open by default. I have made a custom menu item that will
import some table information from text files. The form has connections
to several tables via some combo boxes and listboxes. When I do the
import with this form open I get several errors. So I figure if I close
the form, import the text files, then open the form all is well.

Or am I missing something?

Thanks for your support,
Fred
 
Top