vba 2427 你输入了一个没有值的表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25531146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
2427 You entered an expression that has no value
提问by Chin
I have a function to split up my report into separate PDF files based on a field id
:
我有一个功能可以根据字段将我的报告拆分为单独的 PDF 文件id
:
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
However, while running, I would get this error/warning:
但是,在运行时,我会收到此错误/警告:
2427 You entered an expression that has no value.
2427 您输入的表达式没有值。
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
.
当id
为空时会发生这种情况。我什至不知道这是错误还是警告。单击“确定”后,该函数将继续运行到最后,没有任何问题,输出文件将命名为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.
我试图抑制警告使用,DoCmd.SetWarnings False
但它不起作用。
How can I fix this or at least suppress it so that it doesn't show up?
我该如何解决这个问题或至少抑制它以使其不出现?
回答by RBarryYoung
There's no short-circuiting in VBA, so try changing this:
VBA 中没有短路,所以尝试改变这个:
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