运行Excel VBA循环以执行以下操作: (1)开放Access数据库 (2)在Access数据库中运行宏以导入数据 (3)完全关闭Access数据库
Excel VBA循环在第一次迭代时工作正常但在第二次迭代时在第一次访问宏停止。如果禁用以下2个Excel VBA步骤,则循环根据需要运行(除了数据库未在每个循环结束时关闭): (1)oAccess.DoCmd.Close (2)oAccess.DoCmd.Quit
任何想法如何在每次循环迭代结束时完全关闭数据库(没有shell打开)并在后续迭代中运行宏?感谢。
Dim DataBaseLoc As String
Dim oAccess As Access.Application
For K = First To Last
Set oAccess = New Access.Application
oAccess.Visible = True
If Z = "" Then DataBaseLoc = "C:\AutoInsight.mdb"
If Z <> "" Then DataBaseLoc = "C:\AutoInsight_X.mdb"
oAccess.OpenCurrentDatabase DataBaseLoc
If Z = "" Then Set ObjAccess = GetObject("AutoInsight.mdb")
If Z <> "" Then Set ObjAccess = GetObject("AutoInsight_X.mdb")
With ObjAccess
DoCmd.RunMacro "mcr_Import_Parts"
End With
With ObjAccess
For i = 1 To 2
If i = 1 Then DoCmd.RunMacro "mcr_Import_C"
If i = 2 Then DoCmd.RunMacro "mcr_Import_I"
Next i
End With
oAccess.DoCmd.Close
oAccess.DoCmd.Quit
Next K
答案 0 :(得分:0)
请改为尝试:
End With
Set ObjAccess = Nothing
oAccess.DoCmd.Close
oAccess.DoCmd.Quit
Set oAccess = Nothing
Next K
VBA最终应该注意到ObjAcess
和oAccess
所引用的对象不再有效并处置它们,但通过将变量设置为&#34;可以有助于使事情变得明确。没什么&#34 ;.当变量超出范围时(这种情况就是这种情况),尤其如此