DoCmd.OutputTo not working in MS Access 2010 VBA - ms-access

I'm having a bit of trouble with VBA in MS Access 2010.
My code is attempting to create a new report for each unique Settlement No. I'm then exporting that report to a folder that's created with today's date and the PDF file is assigned a name with the Settlement No.
In the code below, I keep getting an error in the line with the DoCmd.OutputTo method.
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Settlement Reports\" & Format(Date, "mm-dd-yyyy") & "\"
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT dbs_eff_date, batch_id_r1, jrnl_name, ledger, entity_id_s1, account_s2, intercompany_s6, trans_amt, dbs_description, icb_name, [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
If I replace mypath & MyFileName with a hard-coded file path and file name, the macro works so I'm thinking that's where my error is but I can't get it to work correctly.
Any ideas?
Thanks!
Edit:
Here's the specific error:
Run-time error '2501': The OutputTo action was canceled.

You will want to check to make sure the folder exists first, and create it if it doesn't exist. Like this:
If Dir(mypath, vbDirectory) = "" Then MkDir mypath

Related

Not a coder, needing assistance

I am not a coder nor do it work in IT but I maintain an access database for our marketing reporting. This database has a VBA script in it that cycles through our report and creates separate excel files for each entity. I have been able to copy the exact code and change it out using new table and filter references without any issues.
I tried to do it again today and I am getting an error "Run-time error 3061 Too few parameters expected". I have been looking online and everyone seems to say that the error refers to incorrect field names or a missing quote but i can't for the life of me figure it out. I double checked the table and field names and I have tried numerous combinations of quotes with no luck. Sorry if this is a really stupid question but I am stumped.
Private Sub Command4_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Partners"
SQLstring = "SELECT DISTINCT [2-5 User Retest Partners].[Owner] FROM [2-5 User Retest Partners]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Owner")
FilterCriteria = "[2-5 User Retest Partners].[Owner] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Manager Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Partners", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub
This is the version of the code that runs:
Private Sub Command1_Click()
Dim ExportReportName As String
Dim ExportFileName As String
Dim FilterCriteriaString As String
Dim Filter As String
Dim SQLstring As String
Dim rs As DAO.Recordset
ExportReportName = "User Retest-Stores"
SQLstring = "SELECT DISTINCT [2-8 User Retest Stores].[Store Num] FROM [2-8 User Retest Stores]"
Set rs = CurrentDb.OpenRecordset(SQLstring)
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
While (Not rs.EOF)
Filter = rs.Fields("Store Num")
FilterCriteria = "[2-8 User Retest Stores].[Clinic (Default)] LIKE '*" & Filter & "*'"
ExportFileName = "U:\Marketing\Reporting\User Retest Needed\Database Exports\Store Reporting\" & Filter & ".xls"
DoCmd.OpenReport "User Retest-Stores", acViewPreview, , FilterCriteria, acHidden
DoCmd.OutputTo acOutputReport, ExportReportName, acFormatXLS, ExportFileName
DoCmd.Close acReport, ExportReportName, acSaveNo
rs.MoveNext
Wend
End If
Set rs = Nothing
End Sub

How to Change Record Source with VBA in MS Access Report Macro

All,
In MS Access 2010, I have a table (Today's Settled Jrnls) that is linked to a report. I run the VBA code below to export the report to a pdf on a shared drive.
Public Function exporttopdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
mypath = "S:\Dan\" & Format(Date, "mm-dd-yyyy") & "\"
If Dir(mypath, vbDirectory) = "" Then MkDir mypath
Set db = CurrentDb()
Set rs = CurrentDb.OpenRecordset("SELECT distinct [Settlement No] FROM [Today's Settled Jrnls]", dbOpenDynaset)
Do While Not rs.EOF
temp = rs("[Settlement No]")
MyFileName = rs("[Settlement No]") & ".PDF"
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Function
This works but I'd like to change the [Today's Settled Jrnls] table to a different table [New Jrnls]. The new table is has the exact same columns and setup. However, when I change the table in the select statement above, the code runs but the report is blank. I assume this is because the report (Settlement Report) is still linked to the old table. Do you know how I can link the report to the new table with VBA?
Dan
Simply add a RecordSource call to point to new table after first opening report:
' OPEN REPORT
DoCmd.OpenReport "Settlement Report", acViewReport
' ADJUST SOURCE
Reports![Settlement Report].Report.RecordSource = "SELECT * FROM [New Jrnls]"
' FILTER AND OUTPUT
DoCmd.OpenReport "Settlement Report", acViewReport, , "[Settlement No]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "Settlement Report"
You can easily set the Reports recordsource property to a SQL String or bind it to the table
You can even use may of the events like open to set
Me.RecordSource ="SELECT * FROM TBL"
Here check this link: https://learn.microsoft.com/en-us/office/vba/api/access.report.recordsource
For Parfat:

How to output multiple PDF files based on record in MS Access?

I'm new on the MS Access, now I have a report based on the table with 100 records. I want to output the pdf files based on each record. It means each record will have its own single pdf file and the file name will be based on the record's column name.
I have searched on the Internet and I found this vba code is work.
Option Compare Database
Option Explicit
Private Sub Command4_Click()
Dim rsGroup As DAO.Recordset
Dim ColumnName As String, myPath As String
myPath = "C:\test\"
Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT columnName FROM Table_Name", _
dbOpenDynaset)
Do While Not rsGroup.EOF
ColumnName = rsGroup!columnName
' OPEN REPORT, FILTERING RECORDSOURCE BY COLUMN VALUE
DoCmd.OpenReport "Table_Name_Report", acViewPreview, , "Column='" & ColumnName & "'"
' OUTPUT REPORT TO FILE
DoCmd.OutputTo acOutputReport, "Table_Name_Report", acFormatPDF, _
myPath & ColumnName & ".pdf", False
' CLOSE PREVIEW
DoCmd.Close acReport, "Table_Name_Report"
rsGroup.MoveNext
Loop
rsGroup.Close
I never used VBA, and every time I run this code, it will return a window and let me input the column record value. That's not automatically, how to change the code to let it read the record value automatically and populate the pdf?
The way I would do this is to just dynamically set the query the report is based on (i.e. the recordsource) complete with the "columnName". Something like:
(code not run)
Public Sub cmdOpenMyReport_Click()
Dim strSQL As String
Dim rsGroup As DAO.Recordset
Dim ColumnName As String, myPath As String
myPath = "C:\test\"
Set rsGroup = CurrentDb.OpenRecordset("SELECT DISTINCT pharmacyName FROM Sfwy_Patients_New", dbOpenDynaset)
Do Until rsGroup.EOF
ColumnName = rsGroup!pharmacyName
strSQL = "SELECT ... WHERE Column = " & COLUMName & ";" 'copy sql from the report record source and put in the column name as a variable
CurrentDb.QueryDefs("myReportRecordSource").SQL = strSQL
' OUTPUT REPORT TO FILE
DoCmd.OutputTo acOutputReport, "Sfwy_Patients_New_Report", acFormatPDF, _
myPath & ColumnName & ".pdf", False
rsGroup.movenext
Loop
end sub

Access 2013: Saving multiple page PDF from single report

I've never posted before and am new to this. I'm using VBA to get a single report to save multiple PDFs. I receive an "Object missing" error message from the code I built from some of the postings here or other sites. I have tried multiple corrections, but each results in either this or a new error. The code is:
Private Sub RunErrors55COPY_Click()
On Error GoTo Err_RunErrors55_Click
Dim MyPath As String
Dim MyFilename As String
Dim uniqueID As Recordset
Dim Report_ID As Field
Dim Db As Database
MyPath = "\\test.net\files\bissvcs\Services\Errors\"
Set Db = CurrentDb()
Set uniqueID = Db.OpenRecordset("ReportIdent01")
'Loop structure may vary depending on how you obtain values
For Each uniqueID In Report_ID
MyFilename = "Bsvc Error " & Report_ID & ".pdf"
'Open report preview and auto-save it as a PDF
DoCmd.OpenReport "Report01 Error Check", acViewPreview, , "uniqueID = " & Report_ID
DoCmd.OutputTo acOutputReport, "Report01 Error Check", acFormatPDF, MyPath & MyFilename, False
'Close the previewed report
DoCmd.Close acReport, "Report01 Error Check"
Next uniqueID
Exit_RunErrors55_Click:
Exit Sub
Err_RunErrors55_Click:
MsgBox Err.Description
Resume Exit_RunErrors55_Click
End Sub
Any ideas and help would be greatly appreciated!

How to split a report into multiple PDF files

Let's say I have an hundred page report in Access 2010 that includes lists of names (with some other details), grouped by a variable called NOM_RITIRO.
I would like to output the report into different PDF files, one for each value of the variable used for grouping.
I was trying to figure out how to make this code work:
Sub SplitPdf()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim Source As String
Dim SQL As String
Dim MyPath As String
Dim MyFilename As String
MyPath = "D:\Folder\"
Set db = CurrentDb
SQL = "Select NOM_RITIRO From QueryNominativi Group By NOM_RITIRO"
Set rs = db.OpenRecordset(SQL)
While Not rs.EOF
MyFilename = "TK_" & rs!NOM_RITIRO & ".pdf"
' Apply quotes as NOM_RITIRO is a string.
DoCmd.OpenReport "ElenchiNominativi", acViewPreview, , "NOM_RITIRO = '" & rs!NOM_RITIRO.Value & "'"
DoCmd.OutputTo acOutputReport, , acFormatPDF, MyPath & MyFilename, False
DoCmd.Close acReport, "ElenchiNominativi"
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
I got stuck when I try to run the DoCmd.OpenReport.
I get the message box "Enter parameter Value" like if the recordset is not passing any data
Any idea of what I did wrong?
If NOM_RITIRO is not a field in the recordsource of your report called "ElenchiNominativi" (or mis-spelled) then it will prompt you to enter a parameter value.
Similarly, if the recordsource of your report is a query that contains a fieldname not referenced in any of the tables, you will also get this prompt.