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



The information in this article applies to:

  • Microsoft Access 2000

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

  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.

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.

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbhowto kbinfo kbusage KB209938