2012-09-14 78 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之间。 –