ErrorHandler停止在ms访问中捕获错误 [英] ErrorHandler stops traping errors in ms access
问题描述
很抱歉标题不明确,但是我不知道如何用标题来描述我的问题,我的长代码的这一部分触发了保存"对话框:
Am sorry that the title is ambiguous but i didn't know how to describe my problem in the title,any way ,i have this part of my long code that triggers a Save dialog:
Line1:
Dim dlgSaveAs As Object
Dim strFilePath As String
Dim strFileName As String
Set dlgSaveAs = Application.FileDialog(2)
With dlgSaveAs
.InitialFileName = (CurrentProject.Path) & "\Folder" & "_" & Format(Date, "yyyy")
End With
dlgSaveAs.Show
strFilePath = dlgSaveAs.SelectedItems(1)
strFileName = Right(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
ActiveWorkbook.SaveAs FileName:=CurrentProject.Path & "\" & strFileName, FileFormat:=xlOpenXMLWorkbook
SaveError:
Select Case Err.Number
Case 1004 'if it's want to overwrite an old file and i clicked "no" or "cancel"
GoTo Line1 'reopen the Save Dialog
Case 5 'if i clicked "cancel" on the Save Dialog
rs1.Close
Set rs1 = Nothing
MsgBox ("Canceled")
Exit Sub
End Select
如我在代码中所述,
如果ErrorHandler检测到"1004"错误,它将在关闭后重新打开保存"对话框. 每次第一次出现错误"1004"时,处理程序都会正常检测到该错误,但是(通过ErrorHandler)重新打开对话框后,处理程序将停止检测"1004"或"5"中的任何内容. 为什么会这样?
as i explained in the code,if the ErrorHandler detects an '1004' error it will reopen the Save dialog after it was closed. Every time, the first time the error '1004' occurs the handler detects it normally,but after reopening the dialog(by the ErrorHandler),the handler stops detecting anything neither '1004' nor '5'. Why is that ?
推荐答案
听说过吗?
GOTO无效
好吧,GoTo
是邪恶的.
首先,将On Error GoTo SaveError
粘贴在过程的顶部-这将确保错误时正确地跳转 .
First, stick On Error GoTo SaveError
at the top of the procedure - that's what will ensure a proper jump on error.
第一次出现错误1004时,VBA进入错误处理模式",并进入SaveError
子例程.
When error 1004 occurs the first time, VBA enters "error-handling mode", and enters the SaveError
subroutine.
有几种方法可以使VBA脱离错误处理模式"并返回正常执行模式",而无需跳出您所处的过程:
There are several ways to get VBA out of "error-handling mode" and back into "normal execution mode" without jumping out of the procedure you're in:
-
Resume
将重新运行导致错误的语句(在此处注意无限循环!) -
Resume Next
将在导致错误的语句之后运行 next 语句 -
Resume {line label}
将跳到指定的标签
Resume
will re-run the statement that caused the error (watch for infinite loops here!)Resume Next
will run the next statement after the one that caused the errorResume {line label}
will jump to the specified label
请注意,它们全部都包含Resume
关键字.
Notice all of them involve the Resume
keyword.
当您说出GoTo Line1
时,您重新运行该过程,但VBA仍然认为它正在处理错误.
When you say GoTo Line1
, you re-run the procedure, but VBA still thinks it's handling an error.
当运行时认为它正在处理错误时引发的错误肯定不会按照您的预期(您已经注意到)去做.
And an error that's raised while the runtime thinks it's handling an error will definitely not do what you expect it to do (as you've noticed).
用Resume Line1
替换GoTo Line1
.
还考虑在错误处理子例程/标签之前使用Exit Sub
或Exit Function
,以便仅在处于错误状态时才运行该代码块.
Also consider using Exit Sub
or Exit Function
before the error-handling subroutine / label, so as to only ever run that chunk of code when you're in an error state.
最后,考虑使用.Show
函数的返回值来确定对话框是否已取消,而不是依赖于运行时错误.
Lastly, consider using the .Show
function's return value to determine if the dialog was cancelled, instead of relying on runtime errors.
这篇关于ErrorHandler停止在ms访问中捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!