Opening a form already open

G

Guest

I have a switchboard with buttons to open other forms. Here is an example of
On Click code...
DoCmd.OpenForm ("frmCustomers")
Forms.Item("frmSwitchB").Visible = False

The form so opened has a 'return to switchboard button', cmdSwitch, with
code...
Me.Visible = False
Forms.Item("frmSwitch").Visible = True

Everything seems to be working as expected. But... when I click a button on
FrmSwitch, I don't really know if the form it is trying to open is already
open but just not visible. Does it matter? Does Opening a non-visible but
already open form just result in making it visible? (or am I opening the
form yet again, not knowing that I'm building up a collection of invisible
copies)
 
G

Guest

Here is a function that will tell you if the form is already open:

Public Function IsLoaded(ByVal strFormName As String) As Boolean
' 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

Just call the function like this:

If IsLoaded("frmYourFormName") Then
Forms![frmYourFormName].Visible = True
Else
DoCmd.OpenForm "frmYourFormName"
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