Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PrintOrPreview function (free code!) 1

Status
Not open for further replies.

BSman

Programmer
Apr 16, 2002
718
US
Here's the code for a function to print (or preview) a report. I've used this successfully in a number apps. I hope that the lines come out correctly here.

Public Function PrintOrPreview(ReportName As String, Optional strFilter As String, _
Optional strFile As String, Optional AlternateReportName As String, Optional Warning As String) As Boolean
' returns 1 when completed ReportName is name of report to print, strFilter is filter to use to print report
' give user option of canceling [2], printing immediately [7] or previewing on screen first [1]
' if the value DoNotAsk is passed to strFile, then user will NOT be asked if report should be sent to file
' if user selects file option, a file location dialog box automatically comes up as part of the output statement _
and strFile is shown as the default file name
' AlternateFileName is the report to use when sending to a file, since filtering is not available
' Warning is the message to show to the user when sending to a file (example: All data will be reported)
On Error GoTo Err_PrintOrPreview
Dim intPrintHow As Integer
Dim Response As Integer
Dim strPath As String
Dim strFileName As String
intPrintHow = MsgBox("Preview report on screen?", vbYesNoCancel, "Print or Preview Report")
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
' allow sending report to a file only when the value "DoNotAsk" has NOT has been passed to this function
If strFile <> &quot;DoNotAsk&quot; Then
intPrintHow = MsgBox(&quot;Send to file?&quot;, 259, &quot;Send to file or print&quot;) ' Yes/No/Cancel with No as default
If intPrintHow = 2 Then
Exit Function
End If
If intPrintHow = 7 Then
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
Else
If intPrintHow <> 6 Then
Exit Function
Else
If intPrintHow = 6 Then
' note report is NOT filtered when sent to file
If Not IsNull(Warning) Then
Response = MsgBox(Warning, vbOKCancel, &quot;Warning when sending report to a file&quot;)
If Response <> 1 Then ' 1 returned when user clicks OK
MsgBox &quot;Report cancelled!&quot;, vbExclamation
Exit Function
End If
End If
' ask user for path to save file and then ask for file name (showing default name)
strFileName = InputBox(&quot;Enter name of file (must end in .RTF)&quot;, &quot;Name of file&quot;, strFile)
If IsNull(strFileName) Then
MsgBox &quot;Report cancelled -- no file name specified!&quot;, vbExclamation
Exit Function
End If
If Len(strFileName) < 4 Or MID$(strFileName, Len(strFileName) - 3, 4) <> &quot;.RTF&quot; Then
strFileName = strFileName & &quot;.RTF&quot;
End If
strPath = InputBox(&quot;Where should the file be saved (enter the path)?&quot;, &quot;Location to save file&quot;)
If IsNull(strPath) Or strPath < &quot; 0&quot; Then
MsgBox &quot;Report cancelled -- no path specified!&quot;, vbExclamation
Exit Function
End If
If MID$(strPath, Len(strPath), 1) <> &quot;\&quot; Then
strPath = strPath & &quot;\&quot;
End If
strFile = strPath & strFileName
If IsNull(AlternateReportName) Then
DoCmd.OutputTo acOutputReport, ReportName, acFormatRTF, strFile
Else
DoCmd.OutputTo acOutputReport, AlternateReportName, acFormatRTF, strFile
End If
Else
MsgBox &quot;Report cancelled!&quot;, vbExclamation
End If
End If
End If
Else
DoCmd.OpenReport ReportName, acViewNormal, , strFilter
End If
Else
DoCmd.OpenReport ReportName, acViewPreview, , strFilter
End If
Exit_PrintOrPreview:
Exit Function
Err_PrintOrPreview:
Select Case Err
Case 2501 ' message about invoking Cancel action (to be ignored)
Resume Exit_PrintOrPreview
End Select
MsgBox Err.Description
Resume Exit_PrintOrPreview
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top