Function Check If Form is Loaded()

Function Check If Form is Loaded

VBA Code:
Public Function IsFormLoaded(strFrmName As String)As Boolean
 ' Determines if a form is loaded.
 Const conFormDesign = 0
 Dim intNum As Integer
 IsFormLoaded = False
 For intX = 0 To Forms.Count - 1
     If Forms(intNum).FormName = strFrmName Then
        If Forms(intNum).CurrentView <> conFormDesign Then
           IsFormLoaded = True
           Exit Function ' Quit function once form has been found.
        End If
     End If
End Function
How to Use It:

Put the VBA code above in the Module and call it on the click button anywhere on your Access file where you want to check if this form is loaded before call that form.

If you call certain form that is not opened/loaded then you will get an error. So this function is to prevent the error for calling an inactive form.


if you have a form names “frmCustomer”

then put this code under the click button like:

Private Sub Command2_Click()
If IsFormLoaded("frmCustomer") = True Then
     'MsgBox "form is loaded"
     Forms!frmCustomer.Form.Visible = True
     ' or do some thing else on this form
End If
End Sub

You can also put this function inside another function like:

Function showMainForm()
On Error GoTo errorhandler
If IsLoaded("frmCustomer") = True Then
     'MsgBox "form is loaded"
      Forms!frmCustomer.Form.Visible = True
End If
     Exit Function
End Function

Related posts