如何从Access Report生成PDF文件?

时间:2011-02-02 19:19:16

标签: ms-access pdf-generation

我在尝试将Access报告导出为PDF格式时遇到问题。基本上,我正在使用一个旧的(内置于2001年)Access数据库,该数据库使用表单作为用户界面。目前,您可以将作业从“作业”表单发送到“发票”表单。完成所有作业后,只需单击“全部发票”,指定日期和份数,然后使用Access报告作为模板进行打印。

我的任务是添加一个保存为PDF的功能,但作为一名网页设计师,我对Access和VB的知识非常有限,但我确实知道一个(非常)小ASP.Net和C#的数量(我如何得到这个任务是另一次的故事...)

在我看来,我通过在Access窗体上创建一个新的PDF按钮来打印发票。我的想法是,我可以简单地复制打印代码并更新以输出到PDF文件。我可以做到这一点,但不是我喜欢的方式。

打印功能的代码如下:

Private Sub cmdOpenGroupInvoice_Click()
Dim db As DAO.Database
Dim rsGetCustomerInvoice As DAO.Recordset
Dim rsInvoice As DAO.Recordset
Dim rsInvoiceAll As DAO.Recordset
Dim lngCusID As Long
Dim lngJobNo As Long
Dim iCountInvoice
Dim lngInvoiceNo As Long
Dim iNumberCopies As Integer
Dim sSQLGetInv As String
Dim sSQLInv As String
Dim datInvoiceDate As Date

sSQLGetInv = "SELECT tblJobs.JobNo,tblJobs.NetDespatchRef, tblLoads.Sales, tblLoads.PODName, tblLoads.TotalSales, tblLoads.Cost, tblLoads.Profit, tblJobs.SendToInvoice, tblJobs.Invoiced, tblJobs.MarkForHistory, tblJobs.CustomerID" & vbCrLf _
& "FROM tblJobs INNER JOIN tblLoads ON tblJobs.JobNo = tblLoads.JobNo" & vbCrLf _
& "WHERE (((tblJobs.SendToInvoice)=Yes) AND ((tblJobs.Invoiced)=No) AND ((tblJobs.MarkForHistory)=No));"


Set db = CurrentDb
Set rsGetCustomerInvoice = db.OpenRecordset(sSQLGetInv, dbOpenDynaset)
If rsGetCustomerInvoice.EOF Then
Beep
If MsgBox("There are no jobs to invoice", _
    vbCritical + vbOKOnly, _
    "No Jobs To Invoice") = vbOK Then
    Exit Sub
End If
End If
rsGetCustomerInvoice.MoveLast
Debug.Print rsGetCustomerInvoice.RecordCount
rsGetCustomerInvoice.MoveFirst
Do Until rsGetCustomerInvoice.EOF = True
Set rsGetCustomerInvoice = db.OpenRecordset(sSQLGetInv, dbOpenDynaset)
If rsGetCustomerInvoice.EOF Then
    rsGetCustomerInvoice.Close
    db.Close
Set rsGetCustomerInvoice = Nothing
Set db = Nothing
    DoCmd.Close acForm, "frmInvoiceDate"
Exit Sub
End If
Debug.Print rsGetCustomerInvoice.RecordCount
datInvoiceDate = CVDate(txtInvoiceDate)
lngInvoiceNo = GiveMeAnInvoiceNo()
lngCusID = rsGetCustomerInvoice.Fields!CustomerID
Call AddNewInvoice(lngInvoiceNo, datInvoiceDate, True)

Debug.Print iCountInvoice
lngJobNo = rsGetCustomerInvoice![JobNo]
Call SendThisJobToSageAll(lngCusID, datInvoiceDate, lngInvoiceNo)
Call InvoiceAll(lngCusID, lngInvoiceNo)
Dim strPODName As String
If Not IsNull(rsGetCustomerInvoice!NetDespatchRef) Then
If IsNull(rsGetCustomerInvoice![PODName]) Then
strPODName = " "
Else
strPODName = rsGetCustomerInvoice![PODName]
End If
'Call NetDesTrackingJobCompleate(rsGetCustomerInvoice![NetDespatchRef], rsGetCustomerInvoice![JobNo], strPODName)
End If
iCountInvoice = iCountInvoice - 1
'Debug.Print I
iNumberCopies = txtNumberOfCopies
Do Until iNumberCopies = 0

    DoCmd.OpenReport "rptInvoice2", acViewNormal, , "[Invoice No]= " & lngInvoiceNo
iNumberCopies = iNumberCopies - 1
Loop
Form_frmInvoicing.Requery
rsGetCustomerInvoice.MoveNext
Loop
DoCmd.Close acForm, "frmInvoiceDate"
rsGetCustomerInvoice.Close

db.Close
Set rsGetCustomerInvoice = Nothing

Set db = Nothing


End Sub

根据我上面概述的原始计划,我更新了以下部分以输出为PDF:

Do Until iNumberCopies = 0

        DoCmd.OpenReport "rptInvoice2", acViewNormal, , "[Invoice No]= " & lngInvoiceNo
        DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, True
    iNumberCopies = iNumberCopies - 1
    Loop

现在这个工作,它确实尝试输出PDF文件。麻烦的是,它运行报告并为系统中的每个作业创建发票,而不是将报告应用于标记为开票的作业。

我希望这种情况正在发生,因为我把代码放在错误的位置,但我有一种直觉,认为它比那更复杂。

在这里发布它有点长篇大论,但我非常感谢此时的任何帮助。我也试图尽可能地保持这个,所以如果有任何细节不清楚,我会帮忙。

2 个答案:

答案 0 :(得分:4)

这是非常令人费解的,所以我认为最简单的事情是,如果你不想深入和整理,就是修改报告所基于的查询。

Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("MyReportQuery")
sSQL = "SELECT Whatever FROM MyTable WHERE [Invoice No]= " & lngInvoiceNo
qdf.SQL = sSQL

DoCmd.OutputTo acOutputReport, "rptInvoice2", acFormatPDF, _
    MyPath & MyFilename, True

除非您使用Save as PDF Add-on或2010的Access 2007,否则最好安装say,cutePDF,并使用DoCmd.PrintOut打印到PDF打印机

答案 1 :(得分:1)

几年前我在UtterAccess问过同样的问题。

有一个免费的图书馆可以完全满足您的需求:http://www.lebans.com/reporttopdf.htm

我在UA的原始主题是:http://www.utteraccess.com/forum/Automatically-PDF-send-t1353547.html

我在几个项目中成功使用了这个库几年,主要用于生成报价和发票。

希望这会有所帮助 PG

相关问题