Microsoft KB Archive/209938

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

Article ID: 209938

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q209938



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
Microsoft Access allows you to create temporary instances of reports and forms in Visual Basic code by using the "New" keyword. This functionality allows 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.



MORE INFORMATION
 Start Microsoft Access, and then open the sample database Northwind.mdb or the sample project NorthwindCS.adp. Create a new report based on the Products table using the AutoReport: Tabular Wizard, and then save it as Report1. Open Report1 in Design view. On the View menu, click Code.  Type or paste 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 new module, and then type or paste the following functions 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 or paste 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 Immediate 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">