2011-02-02 166 views
0

我在嘗試將Access報告導出爲PDF格式時遇到問題。基本上,我正在研究一種使用表單作爲用戶界面的舊版(建於2001年)Access數據庫。目前,您可以將作業從「作業」表單發送到「發票」表單。一旦所有工作都在那裏,您只需點擊「發票全部」,指定日期和副本數量,並使用Access報告作爲模板打印。我的任務是添加一個保存爲PDF的功能,但作爲一個網頁設計師,我對Access和VB的知識非常有限,但我確實知道很少量的ASP()。 Net和C#(我是怎麼給這個任務是故事的另一個時間...)如何從Access報告生成PDF文件?

在我的腦海裏,我通過創建他們打印發票的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文件。麻煩的是,它運行報告併爲系統中的每項工作創建一張發票,而不是將報告應用於僅標記爲開具發票的工作。

我希望這種情況發生,因爲我已經把代碼中錯誤的位置,但我有一種感覺:它比這更復雜。

在這裏發佈它是一個很長的鏡頭,但我真的很感謝在這一點上的任何幫助。我也試着儘可能縮短這個時間,所以如果有任何不清楚的細節,我會幫忙的。

回答

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 

除非你有Access 2007年與Save as PDF Add-on年或2010年,你可能是最好的安裝說,cutePDF,印刷用DoCmd.PrintOut

+0

另一種選擇是PDFCreator的,這實際上是自動化通過VBA。我有兩個客戶端使用它,除了升級瑕疵之外,它的工作非常好。 – 2011-02-06 00:35:13