checking to see if a form is open

K

Kev

Is there a way to check to see if a form is open? If so
can someone please tell me how I would use that in code?

Example:

If <Form is open> Then
Close Form
End If

Thanks,
Kev
 
F

Fredg

Kev,
Why not just use
DoCmd.Close acForm, "formName"?
If the form is open it will close.
If the form is not open, nothing will happen.
If for some reason you did get an error, just trap it.

However, you may need to know if a form is open for other reasons, such as
cancelling a report if the associated dialog from has been closed.
So here is the IsLoaded function you can copy into a new module if you have
Access 97 or older:

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 call it from an event:
If Not IsLoaded("FormName") Then
etc.
End If

If you have Access 2000 or later you do NOT need the above function.
Access 2000-2002 includes the IsLoaded function in it's library.
Just use:

If Not CurrentProject.AllForms("Form2").IsLoaded Then
Do something here
Else
Do something else
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top