VB错误捕获

时间:2010-08-27 05:56:14

标签: excel vba

我的代码如下

 conDB.Execute strSQL, recordsAffected
         Sheet1.Cells(intStart, 5) = IIf(recordsAffected > 0, "Success", "Failure")

打印失败的内插我想打印错误说明。我试过了

Sheet1.Cells(intStart, 5) = IIf(recordsAffected > 0, "Success", Err.Des)

但它不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Err对象仅在发生错误后才有值。所以试试这个

Sub MySub(strSQL as string)
   On error goto CATCH 
   conDB.Execute strSQL, recordsAffected
   Sheet1.Cells(intStart, 5) = IIf(recordsAffected > 0, "Success", "Hmm ?")
exit sub 
CATCH:
   Sheet1.Cells(intStart, 5) = Err.Description
   on error goto 0
end sub