Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a function to split up my report into separate PDF files based on a field id:

Public Function PrintList() As String

    Dim MyPath As String
    Dim MyFilename As String
    Dim myId As String
    Dim filter As String
    MyPath = "C:
eports"

    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

However, while running, I would get this error/warning:

2427 You entered an expression that has no value.

This happens when the id is null. I don't even know if this is an error or a warning. After I click Ok, the function continue to runs till the end without any problem, and the output file would be named N.pdf.

Since this message box is not like the usual error message box, and since it doesn't give me the option to debug or end the execution, I guess it is not an error?

I have tried to suppress warning using DoCmd.SetWarnings False but it doesn't work.

How can I fix this or at least suppress it so that it doesn't show up?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

There's no short-circuiting in VBA, so try changing this:

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

to this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...