在其中运行过程后尝试删除BAS模块时出现问题

问题描述:

我在本地保存了一个.bas文件,我将其加载到我的Acces项目中以运行特定的子文件。我使用以下代码加载模块(工作正常):

I have a .bas file saved locally that I load into my Acces project to run a particular sub. I use the following code to load the module (which works fine):


Application.VBE.ActiveVBProject.VBComponents.Impor t FileName:= FilePath& ; CODE_FOLDER& RatesYear& " \" &安培; RatesMonth& " \" &安培;产品名称& " .BAS"
Application.VBE.ActiveVBProject.VBComponents.Impor t FileName:=FilePath & CODE_FOLDER & RatesYear & "\" & RatesMonth & "\" & ProductName & ".bas"



我使用以下循环删除模块:

I use the following loop to remove the module:


For Each Comp In Application.VBE.ActiveVBProject.VBComponents

如果Comp.Name = ProductName则

Application.VBE.ActiveVBProject.VBComponents.Remov e Comp

退出对于

结束如果

下一个
For Each Comp In Application.VBE.ActiveVBProject.VBComponents
If Comp.Name = ProductName Then
Application.VBE.ActiveVBProject.VBComponents.Remov e Comp
Exit For
End If
Next



以上工作正常(两者都添加模块并随后删除它)如果加载后我没有在模块中运行任何代码。第二个我在模块中使用代码,删除循环似乎没有立即删除模块(就像在模块中运行任何代码之前一样)。


其中的过程'enqurity如下:

The above works fine (both adding the module and subsequantly removing it) provided I do not run any code within the module once it is loaded. The second I use code within the module the deletion loop does not seem to remove the module immediately (as it would do prior to running any code in the module).

The procedure in it''s entirity is as follows:


公共财产让产品(产品名称为字符串)


昏暗Comp as Object


''如果已经打开,则关闭房价连接

如果RatesConn.State<> 0然后RatesConn.Close


''删除旧的代码模块

For Each Comp In Application.VBE.ActiveVBProject.VBComponents

如果Comp.Name = ProductName那么

Application.VBE.ActiveVBProject.VBComponents.Remov e Comp

退出

结束如果

下一页


''检索最新的代码模块

Application.VBE.ActiveVBProject.VBComponents.Impor t FileName:= FilePath& CODE_FOLDER& RatesYear& &QUOT; \&QUOT; &安培; RatesMonth& &QUOT; \&QUOT; &安培;产品名称& " .bas"


''建立费率数据库连接并设置产品var

RatesConn.ConnectionString =" Provider = Microsoft.Jet.OLEDB.4.0 ;数据来源= &安培; FilePath& RATES_FOLDER& RatesYear& &QUOT; \&QUOT; &安培; RatesMonth& &QUOT; \&QUOT; &安培;产品名称& " .mdb;"

RatesConn.Open


prvProduct = ProductName


结束物业
Public Property Let Product(ProductName As String)

Dim Comp As Object

''Close rates connection if it is already open
If RatesConn.State <> 0 Then RatesConn.Close

''Remove old code modules
For Each Comp In Application.VBE.ActiveVBProject.VBComponents
If Comp.Name = ProductName Then
Application.VBE.ActiveVBProject.VBComponents.Remov e Comp
Exit For
End If
Next

''Retrieve most recent code module
Application.VBE.ActiveVBProject.VBComponents.Impor t FileName:=FilePath & CODE_FOLDER & RatesYear & "\" & RatesMonth & "\" & ProductName & ".bas"

''Establish rates database connection and set product var
RatesConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & RATES_FOLDER & RatesYear & "\" & RatesMonth & "\" & ProductName & ".mdb;"
RatesConn.Open

prvProduct = ProductName

End Property



真的莫名其妙,如果在加载的BAS中没有运行代码,删除循环会立即删除模块,并且可以加载新版本的ub。如果运行代码,则删除循环不会立即起作用,并且当该过程添加BAS的新版本时,它创建具有1的副本。最后。


我试过用一个简单的docmd.deletobject acmodule代替删除循环,ProductName没有太多运气。


你是只能添加/删除模块,而该模块中没有代码运行?


任何帮助非常感谢!!


谢谢


编辑:只是指出显而易见的,我已经确保加载的bas中的所有代码在尝试删除之前已经完成执行。

Really baffling, if no code is run within the loaded BAS the deletion loop removes the module immediately and a fresh version of the ub can be loaded. If code is run the deletion loop does not work immediately and when the procedure comes to add a fresh version of the BAS it creates a duplicate with "1" on the end.

I have tried substituting the deletion loop with a simple docmd.deletobject acmodule, ProductName without much luck.

Are you only able to add/remove modules whilst no code within that module has been run?

Any help greatly appreciated!!

Thanks

Just to point out the obvious, I have made sure that all code within the loaded bas has finished executing prior to attempting to remove it.

除此之外,如果我尝试删除模块(一旦添加它并且其中的sub已经运行),使用来自即时窗口的docmd.deleteobject,将出现一个错误框,提示该项目无法找到该模块(从附带的屏幕截图中可以看出,它显然存在!)。


我应该将此文件作为另一个奇怪的MS错误提交并尝试查找替代解决方案(在这方面的任何建议都非常赞赏atd)?


干杯





Further to the above, if I try to remove the module (once its been added and the sub within it has been run), using docmd.deleteobject from the immediate window, an error box appears suggesting the project is unable to find the module (where as can be seen from the attached screenshot, it is clearly present!).

Should I file this as another odd MS bug and try to find an alternative solution (any suggestions on that front greatly appreciated)?

Cheers
附加图像
(19.1 KB,301 views)
Attached Images
(19.1 KB, 301 views)


远景是模块实际上已被删除,但环境并不完全是一个它的商品。在模块被删除后立即插入以下代码行,看看会发生什么:
A long shot is that the Module has actually been Deleted, but the Environment is not fully aware of it. Immediately after the Module has been Deleted, insert the following line of code and see what happens:
展开 | 选择 | Wrap | 行号


感谢您的建议,不幸的是它没有用。当我删除模块时,我怀疑你在VBA IDEA的正确行上没有刷新。如果当前子程序已经完成,只有在导入的bas中的进程已经运行时才会刷新它。


我的另一个理论是保存导入的模块。如果我使用表单上的一个退出按钮(一个简单的docmd.quit)Access提示我保存导入的模块。我怀疑我需要在导入后保存模块,以便立即删除?远射,但在我脑海里,它有一半是有意义的!有人知道保存导入模块的功能吗?
Thanks for the suggestion, unfortunately it did not work. I suspect you are on the right lines with the VBA IDEA no refreshing when the module has been deleted. It only seems to refresh once the current sub has finished IF a process within the imported bas has been run.

The other theory I have is with the saving of the imported module. If I use one of the exit buttons on the form (a simple docmd.quit) Access prompts me to save the imported module. I suspect I need to save the module once it has been imported so that it removes instantly? Long shot, but in my head it half makes sense!!! Anyone know the function for saving an imported module?