我有大约 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")
,然后将 appAccess
与OpenCurrentDatabase
和 CloseCurrentDatabase
方法。但这是一个不同的问题 --- 它与您面临的 #7866 错误问题无关。
关于ms-access - 在 Access 的另一个实例中处理 openCurrentDatabase 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267296/