2427您输入的表达式没有任何值

时间:2014-08-27 15:25:31

标签: vba ms-access

我有一个功能,可以根据字段id将报表拆分为单独的PDF文件:

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

1 个答案:

答案 0 :(得分: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