gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:41:57 25 4
gpt4 key购买 nike

我有访问数据库的大约400个完整文件路径和文件名的列表。我在Access中编写了一个VBA程序,该程序遍历列表,在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com