2008-12-01 44 views
9

我用我的计划的第一部分访问VBA:是否有可能恢复的错误处理

上的错误去启动

假设在我的第二部分,我再次使用

上错误继续下一步

第二个错误陷阱不会被激活,因为第一个错误陷阱仍然处于活动状态。在使用第一个错误处理程序之后,有没有办法去激活它?

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 

在执行最后一条语句时,我想忽略错误并转到下一个表,但错误处理似乎不起作用。

回答

3

避免错误几乎总是比较好,而不是处理它们。例如:

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 ' 
4

你需要清除错误。尝试将此代码放入:

If Err.Number > 0 Then 
    Err.Clear 
End If 

您还可以使用Err.Number来处理特定的错误情况。

+0

这似乎并不合适。 如果将excel的第一部分放入单独的子文件中,则此代码有效,但我希望如此。 – tksy 2008-12-01 14:49:46

8

On error goto 0给予手错误处理的Visual Basic(一般消息框)

On error goto label将你的代码重定向到标签:

On error resume next会忽略该错误并继续

Resume next将代码重定向到下一行ER错误引发

这意味着,如

On Error goto 0 
... 
On Error goto 0 

没有意义

如果指令的组合要重定向你必须做一个“误差”指令它是这样的:

Do While Not rs.EOF 

    On Error Resume Next 
    rs2.Open strsql 
    On error Goto 0 

    rs2.moveNext 

Loop 

如果您想将错误重定向到一个标签(用于治疗或其他),然后回去的地方,在出现错误代码,你写的东西像:

On error goto label 
... 
... 
On error goto 0 
exit sub (or function) 

label: 
.... 
resume next 
end function 

但我真的建议你在你的错误管理更严格。你应该首先能够做这样的事情:

Set objexcel = CreateObject("excel.Application") 
objexcel.Visible = True 

On Error GoTo error_Treatment 
wbExists = False 
Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls") 
Set objSht = wbexcel.Worksheets("Sheet1") 
objSht.Activate 
wbExists = True 
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 
    .... 
    'there are a number of potential errors here in your code' 
    'you should make sure that rs2 is closed before reopening it with a new instruction' 
    'etc.' 
Next tdf 

Exit sub 

error_treatment: 
SELECT Case err.number 
    Case **** '(the err.number raised when the file is not found)' 
     objexcel.Workbooks.Add 
     Set wbexcel = objexcel.ActiveWorkbook 
     Set objSht = wbexcel.Worksheets("Sheet1") 
     Resume next 'go back to the code' 
    Case **** '(the recordset cannot be opened)' 
     .... 
     .... 
     Resume next 'go back to the code' 
    Case **** '(whatever other error to treat)' 
     .... 
     .... 
     Resume next 'go back to the code' 
    Case Else 
     debug.print err.number, err.description '(check if .description is a property of the error object)' 
     'your error will be displayed in the immediate windows of VBA.' 
     'You can understand it and correct your code until it runs' 
End select 
End sub 

下一步将是预料中的错误在你的代码,以便Err对象将不会引发。例如,您可以编写这样一个泛型函数:

Public function fileExists (myFileName) as Boolean 

然后,您可以测试您的XLS文件是否存在利用代码中的这一功能:

if fileExists("C:\REPORT3.xls") Then 
    Set wbexcel = objexcel.Workbooks.Open("C:\REPORT3.xls") 
Else 
    objexcel.Workbooks.Add 
    Set wbexcel = objexcel.ActiveWorkbook 
Endif   
Set objSht = wbexcel.Worksheets("Sheet1") 
objSht.Activate 

你不需要你的wbExist变量了...

以同样的方式,你应该预测你的记录集没有记录的情况。在测试之前写下rs.MoveFirst可能会引发错误。你应该写

If rs.EOF and rs.BOF then 
Else 
    rs.moveFirst 
    Do while not rs.EOF 
     rs.moveNext 
    Loop 
Endif