我是否在嵌套的If语句中创建了一个循环?

时间:2016-03-09 04:22:27

标签: access-vba ms-access-2007

我编写此代码以检查现有文件,如果没有现有文件或我选择覆盖该文件,则保存报告的PDF。我认为我没有正确地编写嵌套,我认为我可能已经创建了一个循环。代码在DoCmd.Close行崩溃,但是,我无法弄清楚需要改变什么。我打开的报告应该是我数据库中一条记录的报告。关于DoCmd.Close行的一些内容似乎导致报告尝试打开所有记录。使用GoTo可能也不正确。

感谢您一看。

strFileName = Me.[QuoteNumber]
If Len(Dir("C:\Users\A\Desktop\Safe\Development\Qutoes\Test Quotes\" & strFileName & ".pdf")) > 0 Then
    Response = MsgBox("That file already exists! Should I continue?", vbYesNo, "File Exists!")
    If Response = vbNo Then
        Exit Sub
    Else
        GoTo Continue1
    End If

Else
Continue1:
    strWhere = "[ID] = " & Me.[ID]
    Debug.Print "Quote ID: " & strWhere
    DoCmd.OpenReport "QuotesReport", acViewPreview, , strWhere
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, "C:\Users\A\Desktop\Safe\Development\Qutoes\Test Quotes\" & strFileName & ".pdf"
    DoCmd.Close acReport, "QuotesReport"
End If

此代码在报告的格式化上运行。我有四张类似的支票。我想知道这是否效率不高并导致崩溃?

'See if the optional section has items, if not set height to 0
Dim optionalSQL As String
Dim optionalDB As DAO.Database
Dim optionalRS As DAO.Recordset

optionalSQL = "SELECT Top 1 OptionalItems.ID FROM OptionalItems LEFT JOIN     Quotes On OptionalItems.quoteID = Quotes.ID WHERE Quotes.QuoteNumber = " & "'" &      Me.[reportQuoteNumber] & "'"


Set optionalDB = CurrentDb
Set optionalRS = optionalDB.OpenRecordset(optionalSQL)

Debug.Print "Optional record count: " & optionalRS.RecordCount

If optionalRS.RecordCount = 0 Then

    Me!OptionalItems_subreport.Height = 0

Else

End If

optionalRS.Close
Set optionalRS = Nothing
Set optionalDB = Nothing

1 个答案:

答案 0 :(得分:0)

你不需要循环:

strFileName = Me.[QuoteNumber]
If Len(Dir("C:\Users\A\Desktop\Safe\Development\Qutoes\Test Quotes\" & strFileName & ".pdf")) = 0 Then
    Response = vbYes
Else
    Response = MsgBox("That file already exists! Should I continue?", vbQuestion + vbYesNo, "File Exists!")
End If

If Response = vbYes Then
    strWhere = "[ID] = " & Me.[ID]
    Debug.Print "Quote ID: " & strWhere
    DoCmd.OpenReport "QuotesReport", acViewPreview, , strWhere
    DoCmd.OutputTo acOutputReport, "", acFormatPDF, "C:\Users\A\Desktop\Safe\Development\Qutoes\Test Quotes\" & strFileName & ".pdf"
    DoCmd.Close acReport, "QuotesReport"
End If