ACC: How to Set Focus and Close Instances of Report Objects

ID: Q161878

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

Microsoft Access enables you to create temporary instances of reports and forms in Visual Basic code by using the New keyword. This functionality enables you to create an instance of an object on demand.

A new instance of a form or report keeps the original object's Name property, which can make it difficult to refer to a particular instance of the object in code.

This article demonstrates how to set the focus on an instance of a report and then to close it using Windows application programming interface (API) procedures and Visual Basic for Applications code.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

1. Open the sample database Northwind.mdb.

2. Create a new report based on the Products table using the AutoReport:

   Tabular Wizard, and then save it as Report1.

3. Open the Report1 report in Design view.

4. On the View menu, click Code.

5. Type the following procedures in the report's class module:

      Public Function MySetFocus()
         Dim Result As Long
         Result = SetFocusAPI(Me.hWnd)
      End Function

      Public Function MyClose()
         Dim Result As Long
         Result = CloseWindowAPI(Me.hWnd)
      End Function

6. Close the module, and then save and close Report1.

7. Create a module and type the following in the Declarations section:

      Declare Function SetFocusAPI Lib "User32" _
                                       Alias "SetFocus" _
                                       (ByVal hWnd As Long) As Long

      Declare Function CloseWindowAPI Lib "User32" _
                                       Alias "DestroyWindow" _
                                       (ByVal hWnd As Long) As Long

8. Type the following procedure:

      Sub ReportInstancesDemo()
         Dim i As Integer
         Dim r(1 To 4) As New Report_Report1
         ' Create instances of the reports
         ' with unique captions.
         For i = LBound(r) To UBound(r)
            r(i).Visible = True
            r(i).Caption = "Report" & CStr(i)
         Next i
         ' Set focus and close each report instance.
         For i = LBound(r) To UBound(r)
            r(i).MySetFocus
            MsgBox "Click OK to close report instance Report" & CStr(i)
            r(i).MyClose
         Next i
      End Sub

9. To test this procedure, type the following line in the Debug window,
   and then press ENTER.

      ReportInstancesDemo

   Note that the procedure creates four instances of the Report1 report,
   each with a different caption. Then the procedure sets the focus to each
   report in turn, and prompts you to close the instance of the report.

REFERENCES

For more information about declaring API procedures, search the Help Index for "Declare statement."

Additional query words: New Objects Reports

Keywords          : kbusage PgmObj RptEvent 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998