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 Generate and Print a Report Without Saving It


View products that this article applies to.

This article was previously published under Q203992
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

You may have occasion to produce a report, which afterward, you don't want to save. Examples include reporting on a user's responses from a custom wizard or a report on database information, such as record sources that may cause problems with security settings. The reason you would want to delete the report rather than to save it is because you would add to the size of the database file with each save action. This article shows you how to create such a report.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following sample code does the following:
  • It creates and populates a simple array.
  • It creates a simple report and creates event procedures.
  • It prints the array to the report.
  • It discards the report without saving it.
For the sake of simplicity, the data in this example will be supplied from a single dimension array and the output report will have just one data field.
  1. Create a new database.
  2. Create a module and type the following lines in the Declarations section if they are not already there:
    Option Compare Database
    Option Explicit
    Public arySampleData() As Variant
    Public strReportName As String
    					
  3. Type the following procedure:
    Public Sub FillArray()
       Dim aryItem As Long
       For aryItem = 0 To 9
          ReDim Preserve arySampleData(aryItem)
          arySampleData(aryItem) = "Item " & aryItem
       Next aryItem
    End Sub
    					
  4. Type the following procedure:
    Sub TempReport()
      Dim rpt As Report
      Dim mdl As Module
      Dim strCode As String
      Dim lngReturn As Long
      Dim ctlLabel1 As Control, ctlText1 As Control
      Dim intDataX As Integer, intDataY As Integer
      Dim intLineCount As Integer, intCounter As Integer
    
      ' Turn off the screen updating. Comment this line out during
      ' testing because it prevents user interaction if the code fails.
      Application.Echo False
    
      ' Create new report and store the name.
      Set rpt = CreateReport
      strReportName = rpt.Name
    
      ' Expose the report header.
      DoCmd.RunCommand acCmdReportHdrFtr
      Set mdl = rpt.Module
    
      intLineCount = mdl.CountOfLines
      strCode = "Dim intCounter as Integer"
      mdl.InsertLines intLineCount, strCode
    
      lngReturn = mdl.CreateEventProc("Print", rpt.Section(acHeader).Name)
      strCode = "intCounter=0" & vbCrLf & "FillArray"
      mdl.InsertLines lngReturn + 1, strCode
    
      lngReturn = mdl.CreateEventProc("Print", rpt.Section(acDetail).Name)
      strCode = "Me!txtTest.Value= arySampleData(intCounter)" & vbCrLf
      strCode = strCode & "Me!lblTest.Caption= ""Array Value""" & vbCrLf
      strCode = strCode & "If intCounter <> UBound(arySampleData)" _
         & "Then Me.nextrecord=False" & vbCrLf
      strCode = strCode & "intCounter=intCounter+1"
      mdl.InsertLines lngReturn + 1, strCode
    
      ' Set positioning values for report controls.
      ' Measurements are in twips.
      intDataX = 500
      intDataY = 50
    
      ' Create unbound default-size label and text box in detail section.
      rpt.Section(acDetail).Height = 400
      Set ctlLabel1 = CreateReportControl(reportname:=strReportName, _
         ControlType:=acLabel, Left:=intDataX, Top:=intDataY, _
         Width:=1440, Height:=240)
      ctlLabel1.Name = "lblTest"
      Set ctlText1 = CreateReportControl(reportname:=strReportName, _
         ControlType:=acTextBox, Left:=(intDataX + 1440), Top:=intDataY)
      ctlText1.Name = "txtTest"
    
      ' Turn the screen back on.
      Application.Echo True
    End Sub
    					
  5. Type the following function:
    Function BuildReport()
      ' Call the TempReport procedure.
      TempReport
    
      ' Print the report without showing it in preview mode.
      DoCmd.OpenReport strReportName, acViewNormal
    
      ' Close the report without saving it.
      DoCmd.Close acReport, strReportName, acSaveNo
    End Function
    					
  6. To test this function, type the following line in the Immediate window, and then press ENTER:
    ?BuildReport()
    					
Note that the report is printed, and then the report no longer exists. Also note that the variable, intCounter, is initialized and the function, FillArray, is called from the Print event of the ReportHeader section rather than from the Open event of the Report, which would cause the example to fail.

↑ Back to the top


References

For more information about arrays, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Using Arrays in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about recordsets, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Recordsets collection in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about the NextRecord property, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type MoveLayout, NextRecord, PrintSection Properties in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB203992, kbvba, kbprogramming, kbofficeprog, kbhowto, kbdta

↑ Back to the top

Article Info
Article ID : 203992
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 358