Access VBA:是否可以重置错误处理 [英] Access VBA: Is it possible to reset error handling
本文介绍了Access VBA:是否可以重置错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在程序的第一部分中使用
I am using in the first part of my program
出错时开始
假设在第二部分中我再次使用
Suppose in my second part I am again using
出现错误时,继续
由于第一个错误陷阱仍处于活动状态,因此不会激活第二个错误陷阱.使用第一个错误处理程序后,有什么方法可以使其停用?
This second error trap will not get activated as the first one will still be active. Is there any way to de-activate the first error handler after it has been used?
Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True
On Error GoTo Openwb
wbExists = False
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
objSht.Activate
wbExists = True
Openwb:
On Error GoTo 0
If Not wbExists Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
End If
On Error GoTo 0
Set db = DBEngine.opendatabase("C:\book.mdb")
Set rs = db.OpenRecordset("records")
Set rs2 = CreateObject("ADODB.Recordset")
rs2.ActiveConnection = CurrentProject.Connection
For Each tdf In CurrentDb.TableDefs
If Left(tdf.Name, 4) <> "MSys" Then
rs.MoveFirst
strsql = "SELECT * From [" & tdf.Name & "] WHERE s=15 "
Do While Not rs.EOF
On Error Resume Next
rs2.Open strsql
在执行最后一条语句后,我想忽略该错误并转到下一个表,但是错误处理似乎不起作用.
Upon execution of the last statement I want to ignore the error and move on to the next table but error handling does not seem to work.
推荐答案
几乎总要避免出错而不是处理错误.例如:
It is nearly always better to avoid errors, rather than handling them. For example:
Set objexcel = CreateObject("excel.Application")
objexcel.Visible = True
'On Error GoTo Openwb '
'wbExists = False '
If Dir("C:\REPORT3.xls") = "" Then
objexcel.Workbooks.Add
Set wbexcel = objexcel.ActiveWorkbook
Set objSht = wbexcel.Worksheets("Sheet1")
Else
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls")
Set objSht = wbexcel.Worksheets("Sheet1")
End If
objSht.Activate
'wbExists = True '
这篇关于Access VBA:是否可以重置错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文