Microsoft KB Archive/188859

= ACC: How to Generate and Print a Report Without Saving It =

Article ID: 188859

Article Last Modified on 1/23/2007

-

APPLIES TO


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

-



This article was previously published under Q188859



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

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.



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 sample code below 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.  Create a new database.  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   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   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   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   To test this function, type the following line in the Debug window, and then press ENTER: ?BuildReport Note that the report is printed, and then the report no longer exists. </ol>

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.

<div class="references_section">