一直对这个不太清楚,帮助里似乎也没有说,于是今天作了个测试
1、On Error 可作用到被调用的子函数中
在调用过程前使用on error ,发现对过程内的执行起作用。代码
'
注:数据库中没有 abcd 此表,所以正常情况下应该会报错。
sub
main
on error resume next
Conn.Execute ( " select * from abcd " ) ' '忽略错误
test ''忽略错误
end sub
' '另一个文件中
Public Sub Test()
Conn.Execute ( " select * from abcd " )
End Sub
on error resume next
Conn.Execute ( " select * from abcd " ) ' '忽略错误
test ''忽略错误
end sub
' '另一个文件中
Public Sub Test()
Conn.Execute ( " select * from abcd " )
End Sub
2、过程中的on error 语句,对过程外不起作用。代码:
sub
main
test ' '忽略错误
Conn.Execute ( " select * from abcd " ) ' '出现错误
end sub
' '另一个文件中
Public Sub Test()
on error resume next
Conn.Execute ( " select * from abcd " )
End Sub
test ' '忽略错误
Conn.Execute ( " select * from abcd " ) ' '出现错误
end sub
' '另一个文件中
Public Sub Test()
on error resume next
Conn.Execute ( " select * from abcd " )
End Sub
3、on error 语句对调用表单不起作用。例
sub
main
on error resume next
conn.execute " select * from ascd " ' '忽略错误
frm1.show
' 'frm1.show
Private Sub Form_Load()
conn.execute " select * from ascd " ' '出现错误
End Sub
on error resume next
conn.execute " select * from ascd " ' '忽略错误
frm1.show
' 'frm1.show
Private Sub Form_Load()
conn.execute " select * from ascd " ' '出现错误
End Sub
总结:on error 语句有效范围为到该过程,并对调用的过程内部起作用。form不是调用而是另一个过程,所以不会对之前的on error 语句有反应。