Microsoft Office 2000/Visual Basic Programmer's Guide   

Referring to Open Objects

The Application object has properties that return collections of open Access objects. The Reports property returns a reference to the Reports collection that contains all currently open reports. The Forms property returns a reference to the Forms collection that contains all currently open forms. The DataAccessPages property returns a reference to the DataAccessPages collection that contains all currently open data access pages. You specify a member of a collection by using its name or its index value in the collection. You typically use the index value only when iterating through all the members of a collection because the index value of an item can change as items are added to or removed from the collection. For example, the following sample uses the form's name to reference the open Customers form:

Dim rstCustomers As ADODB.Recordset
Set rstCustomers = Forms("Customers").Recordset

The next example closes and saves all open data access pages by looping through the DataAccessPages collection:

For intPageCount = DataAccessPages.Count - 1 To 0 Step -1
   DoCmd.Close acDataAccessPage, _
      DataAccessPages(intPageCount).Name, acSaveYes
Next intPageCount

The Forms, Reports, and DataAccessPages collections contain only open objects. To determine if an object is open, you can use the IsLoaded property of an item in the AllForms, AllReports, or AllDataAccessPages collections, or you can use the SysCmd method with the acSysCmdGetObjectState constant. You can also use the CurrentView property to determine if a form is open in Design, Form, or Datasheet view or if a data access page is open in Design or Page view. The following procedure uses the SysCmd method and the CurrentView property to determine if a form is open in Form or Datasheet view:

Function IsLoaded(ByVal strFormName As String) As Boolean
   ' Returns True if the specified form is open in Form view or Datasheet view.

   Const OBJ_STATE_CLOSED = 0
   Const DESIGN_VIEW = 0

   If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> OBJ_STATE_CLOSED Then
      If Forms(strFormName).CurrentView <> DESIGN_VIEW Then
         IsLoaded = True
      End If
   End If
End Function

The IsLoaded procedure is available in the modIsLoaded module in Solutions9.mdb in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.