ms-access - 在 Access 的另一个实例中处理 openCurrentDatabase 时出现错误

标签 ms-access error-handling vba

我有大约 400 个 Access 数据库的完整文件路径和文件名的列表。我在 Access 中编写了一个 VBA 程序,该程序循环 Access 列表,在 Access 的第二个实例中打开每个数据库,并提取我需要的信息,但是某些文件路径错误并导致 Access 无法打开该数据库的错误。数据库。

尽管我有一个错误处理程序来尝试处理此问题,但错误处理程序不会触发,而是代码在出错时中断。我的选项设置为仅在未处理的错误上中断,但这没有帮助。

有没有办法捕获 appAccess 对象生成的此错误?

Set rstReportList = Currentdb.OpenRecordset("SELECT * FROM tbl_ReportList")

rstReportList .movefirst

Do Until rstReportList .EOF
    On Error Goto CantRun  '--Never actually triggers

    strDb = rstReportList!Location & rstReportList!FileName

    Set appAccess = CreateObject("Access.Application")
    appAccess.OpenCurrentDatabase strDb  '-- Error 7866 occurs here, code breaks

    For each qdf in appAccess.Currentdb.QueryDefs
        UpdateResults rstReportList!ID, rstReportList!FileName, qdf.name, qdf.SQL, qdf.Type
    Next qdf
    appAccess.DoCmd.Quit
NextReport:
    rstReportList.MoveNext
Loop

Set rstReportList = Nothing
Set appAccess = Nothing

Exit Function

CantRun:
    UpdateResults rstReportList!ID, rstReportList!FileName, "Error", "Error", 999
    Set appAccess = Nothing
    GoTo NextReport
End Function

最佳答案

... some of the filepaths are wrong and result in an error for Access being unable to open the database.

使用Dir()来确认strDb指向一个实际存在的文件。并且不要尝试打开一个不存在的文件。

If Len(Dir(strDb)) > 0 Then
    Set appAccess = CreateObject("Access.Application")
    appAccess.OpenCurrentDatabase strDb
    ' ... and everything else you want to do with appAccess
Else
    ' What should happen when the file does not exist?
End If

实际上,在过程开始时仅执行一次 Set appAccess = CreateObject("Access.Application") ,然后将 appAccessOpenCurrentDatabaseCloseCurrentDatabase 方法。但这是一个不同的问题 --- 它与您面临的 #7866 错误问题无关。

关于ms-access - 在 Access 的另一个实例中处理 openCurrentDatabase 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267296/

相关文章:

sql-server - MS-Access 查询到 T-SQL

ms-access - 如何从查询中输出 bool 值

ms-access - 将 Access 97 文本字段的默认值设置为空字符串

Excel VBA : make part of string bold

javascript - 使用可选组获取最短匹配的问题

sql - 为什么这个非常简单的 SQL 查询在 MS Access 中失败?

ruby-on-rails - 开始救援未捕获错误

r - 如何修复R的错误 “missing value where TRUE/FALSE needed”?

javascript - VSt上带有Node.js的“Error: Failed to detect OS”

vba - VBA中<>是什么运算符