如果表单失去焦点,则访问rs停止

时间:2015-02-13 05:53:14

标签: vba ms-access

我有附带代码的Access Form,它通过列表记录来生成报告。但是,当您运行它时,如果弹出另一个对话框或您在另一个应用程序上工作,代码将停止,表单将无响应。任何人都可以建议解决这个问题。代码如下:

Private Sub run_reports_Click()
    Dim DB As DAO.Database
    Dim RS As DAO.Recordset
    Dim MyFileName1 As String
    Dim MyFileName2 As String
    Dim mypath As String
    Dim temp As String
    Dim strFirstName As String
    Dim strLastName As String
    Dim strWeekNumber As String

    mypath = "\\perfpaup04\common\z Head Office\HO Reporting\Sales\" 'Adjust the path to your location
    strWeekNumber = Forms!frm_Sales_Reports!WeekNo

    Set DB = CurrentDb()
    Set RS = DB.OpenRecordset("tbl_BDM_Budgets", dbOpenDynaset) 'Change the table name as required

    Do While Not RS.EOF
        temp = RS("ClientAltNo") 'Change field name as required

        MyFileName1 = RS("FirstName") & " " & RS("Surname") & " - Monthly Data-Week " & strWeekNumber & ".pdf"
        MyFileName2 = RS("FirstName") & " " & RS("Surname") & " - Weekly Data-Week " & strWeekNumber & ".pdf"

        ' set form value to the ClientAltNo Name, so report will be filtered correctly
        Me.List0.Value = RS("ClientAltNo") 'cboName is the combo box name on the form

        ' set the Me.cboName as filter for your report
        DoCmd.OutputTo acOutputReport, "rpt_BDM_ReportData_Summary_Graph", acFormatPDF, mypath & MyFileName1, False
        DoCmd.OutputTo acOutputReport, "rpt_BDM_Revenue_Summary_Weekly", acFormatPDF, mypath & MyFileName2, False

        RS.MoveNext
    Loop
    RS.Close
    Set RS = Nothing
    Set DB = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

您的问题可能是由循环中执行的DoCmd语句引起的。另一种方法是创建Access报告并将其导出为PDF。

DoCmd.OpenReport "ReportName", acViewPreview, , SomeFilter
DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, True
DoCmd.Close acReport, "ReportName"

答案 1 :(得分:0)

尝试在DoEvents行的正上方添加命令Loop

有关详细信息,请参阅http://support.microsoft.com/kb/118468