如何修复运行时错误

时间:2015-08-07 17:01:01

标签: error-handling access-vba

我有一个访问程序,让用户点击一个生成积压报告的按钮,然后显示该报告然后将该报告输出到此路径上的文件夹:
S:\ALC MASTER\Month End Reports - 2015\Senior Management\2015 Backlog Report

但由于某种原因,即使文件正在输出到该文件夹​​,即使出现错误,我仍然会遇到运行时错误。我只是想知道是否有办法来抑制这个错误。

这是我的代码:

Dim MonthStart As Integer
Dim MonthEnd As Integer

For i = 1 To DLookup(DMax("[rsu]", "[RSU totals]", ""), "[RSU totals]", "") Step 1
    If DCount("*", "[RSU totals]", "[rsu] = " & i) > 0 Then

        If Date < "#8/01/" & Year(Now) & "#" Then
            setDate = "7/01/" & Year(Now)
            rptName = "Sales Forecast Detail - Jan-Jun"
        Else
            setDate = "13/01/" & Year(Now)
            rptName = "Sales Forecast Detail - Jul-Dec"
        End If

        fileDirName = "S:\ALC MASTER\Month End Reports - " & Year(Now) & "\" & "Senior Management" & _
        "\" & Year(Now) & " Backlog Report" & "\" & Format(DateAdd("m", -1, Date), "mm") & "-" & _
        Format(DateAdd("m", -1, Date), "mmm") & " - " & Format(DateAdd("m", -1, setDate), "mmm") & ".pdf"

        DoCmd.OpenReport rptName, acViewReport, , "[rsu] = " & i
        Reports(rptName).ReportFooter.Visible = Flase
        DoCmd.OutputTo acOutputReport, rptName, "PDF Format(*.pdf)", fileDirName, True
        DoCmd.Close acReport, rptName

    End If
Next

1 个答案:

答案 0 :(得分:0)

似乎我的问题是我正在浏览那些不存在的报告。取出For循环并初始化If语句。