You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Access VBA错误捕获失效求助:On Error GoTo语句无法正常工作

Access VBA「On Error GoTo ThisError」失效排查求助

我的Access VBA代码功能大致正常,但错误捕获语句On Error GoTo ThisError完全不起作用。已经按照指导,把IDE的「Tools\Options\General」设置改成「Break in Class Module」,也试过「Break on Unhandled Errors」,两种设置都没解决问题。

以下是相关代码:

Sub ListFiles(fldObj As Object, Mask As String)

    Dim fl As Object 'File
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim rst As DAO.Recordset
    Set rst = db.OpenRecordset("FileNames", dbOpenDynaset)

    For Each fl In fldObj.files
     
       For i = 1 To 16
          Extn = Right(fl, i)
          If InStr(Extn, ".") > 0 Then
             Extn = Right(Extn, i - 1)
             i = 16
          End If
       Next i
       On Error GoTo ThisError
       If InStr(fl, "$") > 0 _
          Or Extn = "lnk" _
          Or Extn = "ppm" _
          Or InStr(fl, ".") = 0 _
          Or Len(Extn) > 4 _
          Or Extn = "dat" _
          Or CLng(fl.Size / 1024) < 64 _
          Or Extn = "sst" _
          Or Extn = "ldb" _
          Or Extn = "log" _
          Or Extn = "WMF" _
          Or Extn = "json" Then
             SetAttr fl, vbNormal
             Kill fl
             GoTo LoopOut
          End If
ThisError:
       If fl.Name Like Mask Then
          rst.AddNew
          rst("File").Value = Left(fl.Name, Len(fl.Name) - (Len(Extn) + 1))
          If (CStr(Err.Number)) = 0 Then rst("ErrNum&Description").Value = " " Else 
          rst("ErrNum&Description").Value = CStr((Err.Number) & " " & Err.Description)
          rst("KB Size").Value = CLng(fl.Size / 1024) 
          rst("Ext").Value = Extn
          rst("Home").Value = fldObj
          rst.Update
       End If
       Err.Clear
LoopOut:
    Next

End Sub

内容的提问来源于stack exchange,提问作者AnthonyJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:44:57