Thebatfink
Active Member
- Joined
- Apr 8, 2007
- Messages
- 410
Hi, I am scratching my head. For a long time I've been sucessfully printing an Access report via Excel. Recently I have requirement to now export this to PDF instead, so ultimately I am trying to call an Access report to open from Excel, passing a where clause and then export that report to PDF. It seems DoCmd.OutputTo can't take a clause and the solution I keep seeing is to open the report in print preview first and then export it with OutputTo.
My problem is really two fold, but I don't know if the first issue is causing the second. When I call the report, it is still immediately trying to print, but my understanding of acViewPreview is that this should not be the case. This is using MS Access 2013 and the database is mdb format. Here is my code, this is the sub in its entirety:
First Issue
This is always instantly printing the report, but I read acViewPreview should not cause this? I do have code in my report for Report_Load and Report_Open but all this does is unhide a label on the report based on the value of a field. I can not think of anything else which could be triggering the print. What am I doing wrong here? Here is the report code for clarification:
Second Issue
From my original sub above, after the printing happens (which I am trying to stop), this is causing an 'Output To' prompt to appear in Access asking what type of file format to export to, even though I passed the acFormatPDF argument? But I don't know if it's something to do yet with the previous line printing when not expecting it to.
Any guidance would be much appreciated! Thanks.
My problem is really two fold, but I don't know if the first issue is causing the second. When I call the report, it is still immediately trying to print, but my understanding of acViewPreview is that this should not be the case. This is using MS Access 2013 and the database is mdb format. Here is my code, this is the sub in its entirety:
VBA Code:
Private Sub Test()
Dim objAccess As Object
Set objAccess = GetObject(, "Access.Application")
Dim stdocname As String
stdocname = "Specification"
Dim mnumber As String
mnumber = "166767"
With objAccess
.DoCmd.OpenReport stdocname, acViewPreview, ,"[SPECIFICATION] ='" & mnumber & "'", acHidden
.DoCmd.OutputTo acOutputReport, stdocname, acFormatPDF, "C:\Temp\TestReport.pdf"
.DoCmd.Close acReport, stdocname, acSaveNo
End With
Set objAccess = Nothing
End Sub
First Issue
VBA Code:
.DoCmd.OpenReport stdocname, acViewPreview, "[SPECIFICATION] =""" & mnumber & """", acHidden
VBA Code:
Private Sub Report_Open(Cancel As Integer)
On Error GoTo Err
DoCmd.SetWarnings False
If IsNull(Me.[OPERATIONS MGR]) Or IsNull(Me.[PROCESS DEVPT]) Or IsNull(Me.[QA MANAGER]) Then
Me.Label239.Visible = True
Me.Label209.Visible = True
Else
Me.Label239.Visible = False
Me.Label209.Visible = False
End If
DoCmd.SetWarnings True
Exit Sub
Err:
DoCmd.SetWarnings True
MsgBox Err.DESCRIPTION
End Sub
Second Issue
VBA Code:
.DoCmd.OutputTo acOutputReport, stdocname, acFormatPDF, "C:\Temp\TestReport.pdf"
Any guidance would be much appreciated! Thanks.