2012-09-14 35 views
1

出口到PDF優化VBA代碼,我有兩個功能,將打開和保存基於同一標準的2個指出錯誤報告。他們是相同的除了refrences:是從MS Access

Function Export_MLR() 
On Error GoTo Export_MLR_Err 

Dim strReportName As String 

DoCmd.OpenReport "Market Rate Notification Final", acViewPreview 
strReportName = "S:\National Installations\Market Labor Rates\MLR_INV\MLR\" & Format (Reports![Market Rate Notification Final].Market_ID, "00") & " " & Replace(Reports![Market Rate Notification Final].Product_Code, "/", "_") & "-" & "Market Rate Notification Final" & "_" & Format(Date, "mmddyy") & ".pdf" 

DoCmd.OutputTo acOutputReport, "Market Rate Notification Final", "PDFFormat(*.pdf)", strReportName, False, , , acExportQualityScreen 
DoCmd.Close acReport, "Market Rate Notification Final", acSaveNo 

Export_MLR_Exit: 
Exit Function 

Export_MLR_Err: 
MsgBox Error$ 
Resume Export_MLR_Exit 


End Function 

然後,我創建了這個功能來選擇數據,並把它放到桌上,這些報告refrence逐行:

Function MassMarket() 
On Error GoTo MassMarket_ERR 

Dim db As DAO.Database 
Dim rs1 As DAO.Recordset 
Dim rs2 As DAO.Recordset 


'this query creates my rs1 recordset' 
DoCmd.SetWarnings (warningsOff) 
DoCmd.OpenQuery "mass_market", acNormal, acEdit 
DoCmd.SetWarnings (warningsOn) 



Set db = CurrentDb() 
Set rs1 = db.OpenRecordset("Mass_market_Rate_change") 
Set rs2 = db.OpenRecordset("tbl_Form_Auto") 


'this checks and clears any records in rs2' 
If rs2.EOF = False And rs2.BOF = False Then 
rs2.MoveFirst 
rs2.Delete 
End If 
rs1.MoveFirst 



'loop goes through and adds 1 line runs reports saves them and deletes line' 
Do Until rs1.EOF 


    Set rs2 = db.OpenRecordset("tbl_Form_Auto") 
     rs2.AddNew 
     rs2![MarketID] = rs1![MarketID] 
     rs2![Product_ID] = rs1![Product_ID] 
     rs2.Update 
    Call Export_Invoice 
    Call Export_MLR 

    rs1.MoveNext 
    rs2.MoveFirst 
    rs2.Delete 

Loop 

MassMarket_Exit: 
Exit Function 

MassMarket_ERR: 
MsgBox Error$ 
Resume MassMarket_Exit 

End Function 

現在這一切工作像一個魅力,但它創建,每分鐘平均16個.pdf文件,我不得不創建820個.pdf文件(大約50分鐘)。如果這是我能做的最好的,那麼我會接受它,但如果可能的話,希望將這個時間減半。感謝任何和所有的輸入。 NR

+0

只是爲了清楚起見,Export_Invoice和Export_MLR最終使用從tbl_Form_Auto的數據? –

+0

Export_Invoice和Export_MLR使用tbl_Form_Auto打開報告以運行報告,以填充報告。謝謝! –

+0

在做,直到rs1.EOF循環,是necesary每次循環時間查詢「tbl_Form_Auto,或者你可以進入循環前打開記錄?每次循環重複都不可能添加顯著和unneceesary查詢執行時間查詢表to the loop。 – EastOfJupiter

回答

1

在評論表示您的時間大部分是在你的功能,這將報告導出爲PDF度過。我不確定我們是否可以加速這些。

您似乎打開一份報告,引用該報告中的一個值作爲PDF文件名的一部分,然後使用相同的報告名稱調用OutputTo方法將其另存爲PDF。

在這種情況下,我不確定會發生什麼「引擎蓋下」 ......無論是訪問打開報表對象的第二個實例,或者是足夠聰明地看到,你已經有一個實例公開,公正的使用一個代替。

作爲測試,嘗試信號接入使用的第一個報告實例。從爲對象名參數OutputTo方法的在線幫助:

如果你想輸出的活動對象,指定對象的針對對象類型參數類型將該參數留空。

那麼我的建議是嘗試這在您的代碼:

DoCmd.OutputTo acOutputReport, , "PDFFormat(*.pdf)", _ 
    strReportName, False, , , acExportQualityScreen 

如果Access抱怨,與該對象名參數爲空字符串嘗試。

DoCmd.OutputTo acOutputReport, "", "PDFFormat(*.pdf)", _ 
    strReportName, False, , , acExportQualityScreen 

我不知道多少(或者甚至)這個建議會加快你的代碼。但是如果你不能以某種方式加快這些出口功能,我的預感是你希望把總體時間減少一半是一個不穩定的命題。

+0

感謝您花一些時間來嘗試和幫助我。看起來主要時間是關於查詢報告的時間。它每次都在運行一個大規模的查詢。所以我將它更改爲make table,然後在報表查詢中對其進行修改。 make table查詢需要大約5-10秒,但通話功能從大約3-4秒降到1.在我每分鐘得到16個報告之前,現在我應該在每分鐘50-60之間。 –