2014-08-27 67 views
0

我有一個功能,我的報告分成基於字段id單獨的PDF文件的表達式:2427輸入了沒有價值

Public Function PrintList() As String 

    Dim MyPath As String 
    Dim MyFilename As String 
    Dim myId As String 
    Dim filter As String 
    MyPath = "C:\reports\" 

    Dim rs As DAO.Recordset 
    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT id FROM table") 

    'Loop through the set 
    Do While Not rs.EOF 

     If Not IsEmpty(rs!id) And Not IsNull(rs!id) Then 
      myId = rs!id 
     Else 
      myId = "" 
     End If 

     filter = "id = '" & myId & "'" 

     'Open report preview and auto-save it as a PDF 
     DoCmd.OpenReport "myReport", acViewPreview, , filter 

     MyFilename = "N" & myId & ".pdf" 
     DoCmd.OutputTo acOutputReport, "", acFormatPDF, MyPath & MyFilename, False 

     'Close the previewed report 
     DoCmd.Close acReport, "myReport" 

     rs.MoveNext 
    Loop 

End Function 

但是,在運行一段時間,我會得到這個錯誤/警告:

2427您輸入了一個沒有值的表達式。

這發生在id爲空時。我甚至不知道這是錯誤還是警告。點擊確定後,該功能繼續運行直至結束,沒有任何問題,輸出文件將被命名爲N.pdf

由於此消息框不像通常的錯誤消息框,並且由於它沒有給我選項來調試或結束執行,我猜這不是錯誤?

我試圖用DoCmd.SetWarnings False壓制警告,但它不起作用。

我該如何解決這個問題,或者至少抑制它,讓它不出現?

enter image description here

回答

0

有沒有短路的VBA,所以嘗試改變這一點:

If Not IsEmpty(rs!id) And Not IsNull(rs!id) Then 
     myId = rs!id 
    Else 
     myId = "" 
    End If 

此:

If Not rs!Id Is Nothing Then 
    If Not IsNull(rs!id) Then 
     If Not IsEmpty(rs!id) Then 
      myId = rs!id 
     Else 
      myId = "" 
     End If 
    Else 
     myId = "" 
    End If 
Else 
    myId = "" 
End IF 
+0

應該不是第二個'身份識別碼= RS id'是'myId =「」'而不是? – Chin 2014-08-27 16:41:29

+0

即使沒有短路,這些表達式仍然評估爲真或假? – ashareef 2014-08-27 16:45:53

+0

這是真的,我還沒有看到問題來自哪裏。你能否更詳細地爲我解釋一下RBarryYoung? – Chin 2014-08-27 16:49:57