Archive

Posts Tagged ‘Subform’

MS Access: Check If The Normal Form Or Subform Form Is Loaded

April 18th, 2008 1 comment

If you need to check if MS Access form is loaded or not, you can easily check it using the folloing code

If CurrentProject.AllForms("FORMNAME").IsLoaded = True Then
	' DO SOMETHING
End If

This code is good until you are using single form application. This code will fail if the form is used as subform. To search all the forms including subform, I have created a function calling which will search for all the loaded forms and gives the result as boolean value. Here is the example to check if the form is loaded or not

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME")

The function IsFormLoaded will search each and every control within each form to find the form FORMNAME. Of course, this is a overhead to the application so to reduce the amount of search, two optional arguments can be provided which can be very helpful in searching subform. If you already know the parent form name of the subform and the controlname which holds subform within parent form, the time to search subform will be reduced. For example

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME","PARENTFORMNAME")

will search all the controls withing PARENTFORMNAME form while

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME","PARENTFORMNAME","CONTROLNAME")

will search for the CONTROLNAME control within PARENTFORMNAME form to check if the FORMNAME form is loaded.

The above mentioned functions are here.

Public Function IsFormLoaded(strFormName As String,
		Optional LookupFormName As Form,
		Optional LookupControl As Control) As Boolean
    Dim frm As Form
    Dim bFound As Boolean

    If Not (IsMissing(LookupFormName) Or IsNull(LookupFormName)
		Or LookupFormName Is Nothing) Then
        If Not (IsMissing(LookupControl) Or IsNull(LookupControl)
 		Or LookupControl Is Nothing) Then
        On Error GoTo ErrorHandler:
            If LookupControl.ControlType = acSubform Then
                If LookupControl.Form.Name = strFormName Then
                    bFound = True
                End If
            End If
        Else
            Call SearchInForm(LookupFormName, strFormName, bFound)
        End If
    Else
        For Each frm In Forms
            If frm.Name = strFormName Then
                bFound = True
                Exit For
            Else
                Call SearchInForm(frm, strFormName, bFound)
                If bFound Then
                    Exit For
                End If
            End If
        Next
    End If
ErrorHandler:
    IsFormLoaded = bFound
End Function

Public Function SearchInForm(frm As Form, strDoc As String,
 			bFound As Boolean)
On Error GoTo ErrorHandler
    Dim ctl As Control

    For Each ctl In frm.Controls
        If ctl.ControlType = acSubform Then
            If ctl.Form.Name = strDoc Then
            'If ctl.SourceObject = strDoc Then
                bFound = True
            Else
                Call SearchInForm(ctl.Form, strDoc, bFound)
            End If
SkipElement:
            If bFound Then
                Exit For
            End If
        End If
    Next
    Exit FunctionErrorHandler:
            Resume SkipElement
End Function