Access outputto report to pdf with selection: selection sometimes wrong - ms-access

I've got a database that exports invoices to pdf and mails them, using the outputto command for generating the invoices. I've got the outputto in a function that takes the name of the invoice report (strRptName), the filter on invoice number (stropenargs) and the filename containing the invoice number (strFilename) and returns the filename of the PDF if it could be generated.
Public Function PDFRpt(strRptName As String, strFileName As String, strOpenArgs As String) As String
DoCmd.OpenReport strRptName, acViewPreview, , , acHidden, strOpenArgs
If Reports(strRptName).HasData Then
DoCmd.OutputTo acOutputReport, strRptName, acFormatPDF, strFileName & ".pdf", False
PDFRpt = strFileName & ".pdf"
Else
PDFRpt = "Nodata"
End If
DoCmd.Close acReport, strRptName
End function
In the print and mail subroutine, I loop through the invoice headers calling this function. Filename and filter variables are filled in the loop, obviously using the same invoice number from the recordset.
This usually works, but occasionally, the contents of the pdf don't match up with the filter. For example: the filename says it contains invoice number 149, but if you open the pdf, it shows invoice number 153. There is a file with 153 in the name, that also contains invoice number 153.
I suppose it's transient conditions on the network that cause this hiccough, but is there any way I can prevent it? Perhaps using other code to create the pdf's?
Hope that someone can throw some light on this problem. Thanks in advance.
Ingrid

Related

Access VBA to Pass Parameter To Report Based On Query

I am close on having syntax accurate, just one big problem!
The User ID that I am capturing from the input box, is not being passed into the report, so I have to input the user id twice
What needs to be altered in order to achieve this?
Public Function userProd() As Integer
Dim userID As String, saveloc As String, filename As String, reportname As String
'Report Name
reportname = "rpt_UserProduction"
'Setting Save location
saveloc = "C:\Test\"
'Setting filename
filename = "userProd "
'Getting User ID To Process
userID = InputBox("Enter User ID:", "VBA InputBox Function")
'Putting together full save location
saveloc = saveloc + filename + userID + ".pdf"
If userID = "" Then
'Do Nothing and Stop Processing
Exit Function
Else
If IsNumeric(userID) Then
'Preview Report
DoCmd.OpenReport reportname, acViewPreview, , "User_ID=" & userID
'Save As PDF
DoCmd.OutputTo acOutputReport, reportname, acFormatPDF, saveloc, True
'Close Report
DoCmd.Close acReport, reportname
End If
End If
End Function
Is the User_ID field a text field that holds a number? If so, you need to add single quotes to the filter. ... "User_ID='" & userId & "'". If it's not text, are you getting too many records or not enough? Is there another filter being applied when the report loads, or in the underlying report record source that is causing the filter to not work as expected?
If "twice" means once for the inputbox and once for the report, then "User_ID=" in your where condition is either misspelled or excluded from the query you use as source for the report.

DoCmd.OutputTo fails intermitantly on different items within a list - Error 2501

The DoCmd.OutputTo command fails on different items within a list with Error 2501. Pictured below is my subroutine (much code removed). It loops through a list of vendors, sending invoices via email. We are running Access 365 with linked tables to a SQL Server 2008R2. Each time I run the routine, the code fails on a different supplier (sometimes no failures). There is one attachment per outgoing email. Each attachment has a unique file name. Attachments are written to the hard drive to avoid network lag. The report is fairly complex, sometimes calling functions which hit linked tables. Need help with how to make the function more robust to not fail.
Sub EmailInvoices()
InvFilePath = "C:\temp\"
On Error GoTo EmailInvoicesProblem
' loop through the list of suppliers, this invoice type, this date
For x = 1 To SupplierCount
SQLStr3 = "SELECT * from " & RptTypeDtlQry & WhereClause
DoCmd.OpenReport RptName, acViewDesign, , , acHidden
Set Inv_rpt = Reports(RptName)
Inv_rpt.RecordSource = SQLStr3
Inv_rpt.Caption = "Supplier " & rst!supplier_code & " " & InvType & " Invoices"
DoCmd.Close acReport, RptName, acSaveYes
'image the invoice, attach the invoice to the email, and then delete the image
InvFileName = InvFilePath & rst!supplier_code & ".pdf"
TryAgain:
DoCmd.OutputTo acOutputReport, RptName, acFormatPDF, InvFileName, False
objOutlookMsg.Attachments.Add (InvFileName)
fso.DeleteFile (InvFileName)
' send all the invoices of this supplier, this invoice type, this date in a single email.
objOutlookMsg.Send
NextSupplier:
rst.MoveNext
Next x
rst.Close
Exit Sub
End Sub 'EmailInvoices()
Perhaps the report file name has invalid characters? (would have posted this as a comment but don't have enough points)
Can you put some Debug.Print statements in to show what the variables are when it works and when it doesn't?

Export MS Access Report to PDF based on condition

Is there any way to export a report in MS Access to PDF based on a certain criteria/field on the report?
I have created a productivity report in MS Access. Instead of exporting 50 pages into 1 PDF, is there a way to export based on the manager's name? The field for the managers name is included on the actual report.
You can take this idea and play with it. Insert this into a Module
Option Explicit
Dim g_ManagerReportFilterEnabled As Boolean
Dim g_ManagerReportFilter As String
Public Function IsManagerReportFilterEnabled() As Boolean
IsManagerReportFilterEnabled = g_ManagerReportFilterEnabled
End Function
Public Function GetManagerReportFilter() As String
GetManagerReportFilter = g_ManagerReportFilter
End Function
Public Sub ExportFilteredManagerReportToPDF(strManagerName As String)
On Error GoTo ExportFilteredManagerReportToPDF_ErrorHandler
g_ManagerReportFilterEnabled = True
g_ManagerReportFilter = "[MyManagerNameField] = " & Chr(34) & strManagerName & Chr(34)
DoCmd.OutputTo acOutputReport, "MyReportName", acFormatPDF, "MyPath:\MyFileName.PDF", False
GoTo ExitMe
ExportFilteredManagerReportToPDF_ErrorHandler:
Debug.Print err.Number & ": " & err.Description
ExitMe:
g_ManagerReportFilterEnabled = False
Exit Sub
End Sub
and review the variables you need to replace. And this into Report_Open of your report:
Private Sub Report_Open(Cancel As Integer)
If IsManagerReportFilterEnabled = True Then
Me.Filter = GetManagerReportFilter
Me.FilterOn = True
End If
End Sub
So the issue this code is trying to solve is that we want to use DoCmd.OutputTo to output our PDF, but it does not take a Filter parameter. So we work around this by setting up two global variables (I know...) which let us know if we should use the Manager filter and what that filter is. When we run ExportFilteredManagerReportToPDF and pass through a name, the sub will output the report to PDF. Because of the code attached the report, when OutputTo runs, the report will detect whether the filter is enabled, and if it is, apply it. Then OutputTo finishes its work and the PDF is output.
To run this for manager John Smith, say, you can run this from the debug window:
ExportFilteredManagerReportToPDF "John Smith"

Split MS-Access reports into pdf's

I got the following VBA code from the web a while ago:
Private Sub btnCreatePDF_Click()
Dim MyPath As String
Dim MyFilename As String
MyPath = "D:\reports\"
MyFilename = "KS1.pdf"
'Open report preview and auto-save it as a PDF
DoCmd.OpenReport "Rpt_KS1", acViewPreview
DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, False 'Change false to true here to auto-open the saved PDF
'Close the previewed report
DoCmd.Close acReport, "Rpt_KS1"
End Sub
It was for use in MS Access to create a single pdf of reports (containing up to 30 pages) and works fine for what i needed. However, i now need to split the report into the 30 or so pages and create a pdf for each of the pages. Any idea how this can be done?
I have a 'username' in the report or can add a unique ID if this helps to split them etc.
Use the 4th parameter (WhereCondition) of Docmd.OpenReport. With the WhereCondition, do exactly what you would normally do when adding a Where to your query, only don't include the word Where. This will make the report only display records that match the WhereCondition.
Retrieve your list of unique identifiers into some sort of a collection, or recordset then do a loop. This example assumes that you have them in a collection called uniqueIds and will almost definitely require some modification by you.
Dim MyPath As String
Dim MyFilename As String
MyPath = "D:\reports\"
'Loop structure may vary depending on how you obtain values
For each uniqueId in uniqueIds
MyFilename = "KS1" & uniqueId & ".pdf"
'Open report preview and auto-save it as a PDF
DoCmd.OpenReport "Rpt_KS1", acViewPreview, , "uniqueField = " & uniqueID
DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, False
'Close the previewed report
DoCmd.Close acReport, "Rpt_KS1"
Next uniqueId
Strictly speaking, this may not result in a different PDF for each page. But it will generate a different PDF for each uniqueID, which might be every page depending on your data.

Trying to customize a report name when I save it as a PDF in Access 2010

When I run the below code I get an error "Object Required". I want to be able to click a button and it will save this file along with a numerical field on the report known as Market_ID, then name of the report is Market Rate Notification Final. The report does show up but I do not get a save dialog box. Also if I remove
Report![Market Rate Notification Final].Market_ID + from strReportName I get a save dialog box with the file prenamed as Market Rate Notification Final.pdf.
Option Compare Database
'------------------------------------------------------------
' Export_MLR
'
'------------------------------------------------------------
Function Export_MLR()
On Error GoTo Export_MLR_Err
Dim strReportName As String
strReportName = Report![Market Rate Notification Final].Market_ID + "Market Rate Notification Final"
DoCmd.OutputTo acOutputReport, strReportName, "PDFFormat(*.pdf)", "", False, "", , acExportQualityScreen
Export_MLR_Exit:
Exit Function
Export_MLR_Err:
MsgBox Error$
Resume Export_MLR_Exit
End Function
You cannot refer to the contents of a report control like this:
strReportName = _
Report![Market Rate Notification Final].Market_ID + "Market Rate Notification Final"
The result of the various errors is that strReportName is Null. First, it is Reports, not Report, next, you cannot get the value of a control in this way from a report, what you will get is the value of the last row of the report, and the concatenator is & not +, unless you are doing something quite fancy.