Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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).

↑ Back to the top


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.

↑ Back to the top


More information

  1. Start Microsoft Access, and then open the sample database Northwind.mdb or the sample project NorthwindCS.adp.
  2. Create a new report based on the Products table using the AutoReport: Tabular Wizard, and then save it as Report1.
  3. Open Report1 in Design view.
  4. On the View menu, click Code.
  5. 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
    					
  6. Close the module, and then save and close Report1.
  7. 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
    					
  8. 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
    					
  9. 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.

↑ Back to the top


References

For more information about declaring API procedures, click Microsoft Visual Basic Help on the Help menu, type declare statement example in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB209938, kbusage, kbinfo, kbhowto

↑ Back to the top

Article Info
Article ID : 209938
Revision : 4
Created on : 7/13/2004
Published on : 7/13/2004
Exists online : False
Views : 320