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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save report as pdf 1

Status
Not open for further replies.

humpydumpy2000

IS-IT--Management
Aug 18, 2016
30
PH
hi all,

I would like to save an unbound report as pdf, the problem is access is bit confused what I did was to make a command button in a form and embeded the following code in an onClick event:



Dim wsSQL As String
wsSQL = "SELECT * from tblsubject;"
Set db = CurrentDb
Set rs = db.OpenRecordset(wsSQL, dbOpenDynaset, dbSeeChanges)


Reports!rpt!subject = rs.Fields("subject").Value
rs.Close
db.Close

DoCmd.OutputTo acOutputReport, "rpt", "PDFFormat(*.pdf)", "report.pdf", True, "", 0, acExportQualityPrint


access tells me its error 2451: report name rpt doesnt exist. Any ideas please?
 
Is your report actually called "Rpt"?
 
your problem is here
Reports!rpt!subject = rs.Fields("subject").Value

The reports collection like the forms collection only contains the open forms/report

Since the report is not open that line of code is looking for a report in the reports collection, but there is none. Have to open it first.
 
BTW you can open it hidden if you do not want to show it.
 
ok my point is I dont want to open the report I simply want to export it to pdf? Isnt it possible?
 
Yes you can, but as I explained that is not your problem. Your problem is that you are trying to set a value on a closed form first. That you can not do.
Reports!rpt!subject = rs.Fields("subject").Value

If you want to do that open it hidden
Code:
Dim wsSQL As String

wsSQL = "SELECT * from tblsubject;"
Set rs = currentdb.OpenRecordset(wsSQL, dbOpenDynaset, dbSeeChanges)
docmd.openreport "rpt",acpreview,,,achidden
Reports!rpt!subject = rs!subject
DoCmd.OutputTo acOutputReport, "rpt", "PDFFormat(*.pdf)", "report.pdf", True, "", 0, acExportQualityPrint
docmd.close acreport,"rpt"
rs.Close

If you truly wanted it closed, you would have to open it in design view, set the property of the subject field as
Reports!rpt!subject = "= '" & rs!subject & "'"
then save the report and close the report.
Too much work, for what purpose?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top