Microsoft KB Archive/161878

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

Article ID: 161878

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q161878



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



SUMMARY
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
 Open the sample database Northwind.mdb. Create a new report based on the Products table using the AutoReport: Tabular Wizard, and then save it as Report1. Open the Report1 report in Design view. On the View menu, click Code.  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  Close the module, and then save and close Report1.</li>  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 </li>  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 </li> 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.</li></ol>

<div class="references_section">