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

CODE:

CurrentProject.AllForms("YourFormName").IsLoaded



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

CODE:

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_Handler:
      Exit Function

Err_Handler:
      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?



Feedback

Please use the E-Mail button in the contact form below to let me know whether you found this article useful or if you have any questions.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 23 Oct 2022



Return to Code Samples Page




Return to Top