MS-Access Docmd to Open a Report - ms-access

Is there a way to speed up the report creation in MS-Access? Currently I am using
Dim reportName As String
reportName = "RPT_MonthlyTargetCompliance_IMPLEMENTATION"
fileName = folderPath & "myFile.pdf"
DoEvents
DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, fileName
DoEvents
DoCmd.Close acReport, reportName, acSaveNo

Related

(VBA) create a file "pdf" for each record in a table

Help me, this code I wrote, creates multiples files but every file contains only the values from the first record:
I want a file pdf for each record in the table.
Option Compare Database
Option Explicit
Private Declare PtrSafe Sub
Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Private Sub Creazione_Click()
Dim rs As DAO.Recordset
Dim rpt As Access.Report
Dim sFolder As String
Dim sFile As String
Const sReportName = "Mandato"
sFolder = "C:\Users\Famiglia\Desktop\Mandati\"
Set rs = Me.RecordsetClone
With rs
If .RecordCount <> 0 Then
'Open the Report
DoCmd.OpenReport sReportName, acViewPreview, , , acHidden
'Define a report object so we can manipulate it below
Set rpt = Reports(sReportName).Report
.MoveFirst
Do While Not .EOF
'Build the PDF filename we are going to use to save the PDF with
sFile = Nz(![id], "") & " " & Nz(![COGNOME], "") & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, sReportName, acFormatPDF, sFile, , , , acExportQualityPrint
Call Sleep(1000)
DoEvents
.MoveNext
Loop
DoCmd.Close acReport, sReportName
End If
End With
End Sub
Open, output, close report filtered to record within loop.
Do While Not .EOF
DoCmd.OpenReport sReportName, acViewPreview, , "ID=" & rs!ID, acHidden
...
DoCmd.Close acReport, sReportName
.MoveNext
Loop
A report object variable is not necessary. Your code doesn't even use the variable.

Best way to rename an auto-generated report in MS Access

How do you assign a name to a MS Access report created via CreateReport?
Code creates a dynamic report then adds controls to the report. Last, I want to name/save the report as "Student_Scores_Report01" but it appears that CreateReport auto-generates the name for the report as Report1, Report2, etc. I've run into issues where Access creates a "Report1" but it doesn't actually exist, causing the whole project to be corrupted. This is my best guess at working around this but it seems inefficient:
Dim rpt as Report
Set rpt = CreateReport
Dim ReptNmTemp as String
ReptNmTemp = rpt.name
DoCmd.Save acReport, rpt.Name
DoCmd.Close acReport, rpt.Name
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
For Each rpt02 In CurrentProject.AllReports
If rpt02.Name = ReptNmTemp Then
DoCmd.DeleteObject acReport, ReptNmTemp
End If
Next
The For...each loop deletes the auto-generated report and avoids the corruption problem. But is there a better way? Thanks in advance.
Instead of looping you could On Error Resume Next or some other error handler.
Dim rpt as Report
Dim ReptNmTemp as String
Set rpt = CreateReport
ReptNmTemp = rpt.name
DoCmd.Save acReport, ReptNmTemp
DoCmd.Close acReport, ReptNmTemp, acSaveYes
DoCmd.Rename "Student_Scores_Report01", acReport, ReptNmTemp
On Error Resume Next
DoCmd.DeleteObject acReport, ReptNmTemp
Or use acDefault, which is actually a default parameter for both Save and Close, which means if parameter is not provided, acDefault will be used.
Dim rpt as Report
Set rpt = CreateReport
DoCmd.Save , "Student_Scores_Report01"
DoCmd.Close , , acSaveYes

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:

Access Report - show current recordsource of another form

I have a report that displays another form's recordsource. When I add some record on this form and click for report, this records is not being displayed. How can I achieve displaying what is on screen - allways ? Here is my simple code for report:
Private Sub cmdOpenReport_Click()
DoCmd.OpenReport "MyReport", acViewReport
End Sub
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = Forms![MyForm].Form.RecordSource
End Sub
You need to close the Report before re-opening it and save the new record.
In the click event of your Open Report button insert the following:
Private Sub OpenReport_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acReport, "Test1", acSaveYes
DoCmd.OpenReport "Test1", acViewPreview
End Sub
Try requerying your Forms Recordset before opening the report:
Private Sub cmdOpenReport_Click()
Me.FilterOn = False
Me.Requery
DoCmd.OpenReport "MyReport", acViewReport
End Sub
I have an answer, I knew It's not so easy (in click event of OpenReport button):
Dim strWhere As String
Me.Dirty = False
With Me.Recordset.Clone
Do Until .EOF
strWhere = strWhere & "," & !ID
.MoveNext
Loop
End With
strWhere = Mid(strWhere, 2)
DoCmd.OpenReport "MyReport", acViewReport, WhereCondition:="ID In (" & strWhere & ")
Answer provided by Leigh Purvis (Access MVP), a BIG thanks once more !!

How to save an access report with a custom name that is pulled out of the report?

I need to save the PDF file with a custom name. This name should include a parameter/value from the report.
I need to generate several reports like this so I need a differentiation which I guess the Member_ID field will provide. I did search a lot on the web, but I am not able to implement any of it.
The code which I have now looks like this:
Private Sub Create_PDF_Click()
Dim myPath As String Dim strReportName As String
DoCmd.OpenReport "Proof_Graphs", acViewPreview
myPath = "C:\Proofs\"
strReportName = Proof_Graphs.[MEMBER_ID] + "-" + ".pdf"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, myPath + strReportName, True DoCmd.Close acReport, "Proof_Graphs"
End Sub
I am getting an object not found error.
You can use
Reports!ReportName!FieldName to access the control box for the value on the report.
Private Sub Create_PDF_Click()
Dim myPath As String Dim strReportName As String
Dim memberID As String
DoCmd.OpenReport "Proof_Graphs", acViewPreview
memberID = Reports!Proof_Graphs.[MEMBER_ID]'to access the field for the value on the report
myPath = "C:\Proofs\"
strReportName = memberID + ".pdf"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, myPath + strReportName, True DoCmd.Close acReport, "Proof_Graphs"
End Sub