The Linux Page

Check whether a form is open in MS-Access

MS-Access has forms, but no real means to know whether a form exists. It looks like you could go through the list of forms (Forms.item(i)) but I'm not too sure that would work in all circumstances.

Instead, I used code I found on another site which runs a system command to determine whether a form is currently attached to a window (has a state other than zero.)

The code looks like this:

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

The first command checks the state of the form object named strFormName. The second line checks that the current view mode is one that means the window is open / visible in some way.

The result is saved in IsLoaded.

In my case, I needed to test that to eventually retrieve a field from a form if that form is open. Otherwise I could take the parameter from the OpenArgs parameter of the report. Unfortunately, when you send an email you cannot specify anything to fill in the OpenArgs parameter of the report you want to send!

So, my code goes something like this:

Function GetSalesOrderNumber()
    If SysCmd(acSysCmdGetObjectState, acForm, "SendEmail") <> 0 Then
        If Forms("SendEmail").CurrentView <> 0 Then
            GetSalesOrderNumber = Forms("SendEmail").SalesOrderNumber
            Exit Function
        End If
    End If
    GetSalesOrderNumber = Report.OpenArgs
End Function

Note that since I just need that number returned, I don't both with a flag. In any event, since we need to have that other form open to send that specific report as an email, we can do that this way. However, looking at the resulting output, it feels like we won't be able to include images in the attachment so that's probably not going to be necessary (i.e. I'll have to create another report which will always take the sales order number from the Send Email form.)

Source: http://www.access-programmers.co.uk/forums/showthread.php?t=67046