Code Samples for Businesses, Schools & Developers

First Published 23 Oct 2022

Sometimes it is useful to know using code whether a form has been loaded (opened)

This can be done with one line of code which returns True if the form is loaded ; otherwise False



Alternatively, here is a function based on different code which has the same result:


Function IsFormLoaded(ByVal strFormName As String) As Integer

'Returns True if Open or False if form is not open

On Error GoTo Err_Handler

      If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
            If Forms(strFormName).CurrentView <> 0 Then
                  IsFormLoaded = True
            End If
      End If

      Exit Function

      MsgBox "Error " & err & " in IsFormLoaded procedure : " & Err.Description
      Resume Exit_Handler

End Function

Typical usage: close a form if it is open

If IsFormLoaded("YourFormName") Then DoCmd.Close acForm, "YourFormName"

If you need to know whether a form is open as a subform, please see my article: Is Form Open as a Subform?

Please use the contact form below to let me know whether you found this code useful or if you have any questions.

Colin Riddington           Mendip Data Systems                 Last Updated 23 Oct 2022

Return to Code Samples Page

Return to Top