473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem trying to remove BAS module after running a procedure within it

16 New Member
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 .ActiveVBProjec t.VBComponents. Import FileName:=FileP ath & CODE_FOLDER & RatesYear & "\" & RatesMonth & "\" & ProductName & ".bas"
I use the following loop to remove the module:

For Each Comp In Application.VBE .ActiveVBProjec t.VBComponents
If Comp.Name = ProductName Then
Application.VBE .ActiveVBProjec t.VBComponents. Remove Comp
Exit For
End If
Next
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:

Public Property Let Product(Product Name 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 .ActiveVBProjec t.VBComponents
If Comp.Name = ProductName Then
Application.VBE .ActiveVBProjec t.VBComponents. Remove Comp
Exit For
End If
Next

'Retrieve most recent code module
Application.VBE .ActiveVBProjec t.VBComponents. Import FileName:=FileP ath & CODE_FOLDER & RatesYear & "\" & RatesMonth & "\" & ProductName & ".bas"

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

prvProduct = ProductName

End Property
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.deletobje ct 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

EDIT: 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.
May 28 '09
20 5805
NeoPa
32,569 Recognized Expert Moderator MVP
@ADezii
Yes. I shan't ==> I will not.

Sounds like ADezii's curiosity is piqued. That's always good news :)

I will monitor this closely as manipulation of code by code is always interesting (even if quite dangerous in the wrong hands).
May 28 '09 #11
ADezii
8,834 Recognized Expert Expert
@NeoPa
I somehow knew the shan't would get ya! (LOL)!
May 28 '09 #12
FishVal
2,653 Recognized Expert Specialist
Gentlemen.

I've experimented a while with this problem and the only way I could reproduce the bug is when subroutine supposed to remove module contain early compiled call to public subroutine in the module to delete.

e.g.

Module: Module1
Expand|Select|Wrap|Line Numbers
  1. Public Sub Sub1()
  2.     .....
  3. End Sub
  4.  
Module: Main
Expand|Select|Wrap|Line Numbers
  1. Public Sub DelAndImport()
  2.     With Application.VBE.ActiveVBProject.VBComponents
  3.         .Remove .Item("Module1")
  4.         .Import "X:\Module1.bas"
  5.         Sub1
  6.     End With
  7. End Sub
  8.  
on the other hand, indirect call of the sub works flawlessly


Module: Module1
Expand|Select|Wrap|Line Numbers
  1. Public Sub Sub1()
  2.     .....
  3. End Sub
  4.  
Module: Main
Expand|Select|Wrap|Line Numbers
  1. Public Sub DelAndImport()
  2.     With Application.VBE.ActiveVBProject.VBComponents
  3.         .Remove .Item("Module1")
  4.         .Import "X:\Module1.bas"
  5.         Run "Sub1"
  6.     End With
  7. End Sub
  8.  
This, I guess, makes a perfect sense. Since there is a compiled and running code referencing code module, it could not be removed.

So, in few words, the problem is not that the module is loaded since some code in it has been invoked, but that there is a compiled call to this module in code being executed.

Regards,
Fish
May 28 '09 #13
NeoPa
32,569 Recognized Expert Moderator MVP
That makes sense Fish.

late-binding should be fine for such a temporary module I would expect.
May 28 '09 #14
ADezii
8,834 Recognized Expert Expert
Don't forget 2 other viable, and probably more efficient approaches to running Functions/Sub-Routines in External Modules, namely:
  1. Create a Library Database (*.mda/*.mde) consisting of the Module's functionality. You could then set a Reference, either manually or dynamically, to this Library Database and access its Sub-Routines, Functions, Constants, etc...
  2. (Automation) - Set an Object Reference to the Database containing the External Module, then use the Run Method of the Application Object to execute its Routines, etc. This approach, would of course, involve encapsulating the Module's Code within a DB.
  3. I feel as though either approach would be better than the current one.
May 28 '09 #15
Nates
16 New Member
I have managed to delete a BAS then import a new version and then run the sub within the loaded BAS without any trouble. The problem arrives when I try to remove this BAS to load a new one.

The database is a motor insurance quote engine which will need to load one of several hundred BAS files at runtime dependant on the effective month / years rates selected by the user. I have attached a number of screenshots to try to better highlight this. It seems the first time a BAS is removed and a new one loaded everything works fine, however, each subsequant BAS seems to cease any classes that are active (they will appear to be set back to Nothing).

We are limited on space hence why we are not using multiple MDB files to act as code containers.

EDIT: Have uploaded a ZIP with the full size screenshots.

Thanks for all your help so far guys :)
Attached Images
File Type: jpg CodeFiles.jpg (14.1 KB, 374 views)
File Type: jpg MonthYearSelect1.jpg (7.7 KB, 346 views)
File Type: jpg QuoteInfo.jpg (13.3 KB, 334 views)
File Type: jpg PremiumGeneratedByBASSub.jpg (14.0 KB, 329 views)
Attached Files
File Type: zip Screenshots.zip (337.2 KB, 113 views)
Jun 2 '09 #16
FishVal
2,653 Recognized Expert Specialist
Hello, Nates.
  • Did you try indirect call using Application.Run method.
  • What is so different and unpredictable in those calculations that you have to write a separate VBA code for each?
Jun 2 '09 #17
Nates
16 New Member
@FishVal
I've not tried to indirectly call, will give that a go now.

The code generates an insurance premium. Although the same table of risk information is used to generate a premium i.e. they all use Driver's Sex, Age, Vehicle, Cover etc they behave differently when generating underwriting terms based on this information (be it differing excesses, endorsements, premium breakdowns). Some of the product BAS files require little over 200 lines of code to achieve the final premium, others are closer to 2000.
Jun 2 '09 #18
ADezii
8,834 Recognized Expert Expert
@Nates
Any possibility of Uploading a Mini-Version of your DB with only a few Modules, and only the minimal and essential code that is failing? You need only to include the relevant code along with any other specifics. The data itself is also irrelevant, can even be dummy, and need only parallel the actual data in the DB.
Jun 2 '09 #19
Nates
16 New Member
Will see if I can get something uploaded today.

Thanks for continuing to offer assistance, really appreciated.
Jun 3 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

17
14641
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to allow entry to one or the other or Both at the same time. Now I tried to use an If ElseIf but it got too hard to track, so now I am using a SELECT CASE Statement.
5
9549
by: George Copeland | last post by:
This is a request for help fixing a SQL Server 2000/ADO problem on Windows XP. I would appreciate any useful assistance. PROBLEM: SQL Server access on my machine fails as follows: 1. All of my VB6 apps reference the following ADO typelib: Microsoft ActiveX Data Objects 2.7 Library Located at: c:\Program Files\Common Files\System\ADO\msado27.tlb
3
2521
by: Stephanie | last post by:
I have a problem that I am trying to solve. We have a huge product with a whole lot of ASP and VB code. VB code is all ActiveX dlls which are used by ASP app. When I attempt to add features or fix bugs, I like to execute the dlls in debug so I can see what is going on. (Some folks in my org seem to have this kinda wacky bias against debugging, as if *real* programmers should not need a debugger. Whatever, it is the fastest way for me to get...
42
4972
by: WindAndWaves | last post by:
Dear All Can you tell me why you use a class module??? Thank you Nicolaas ---
3
5469
by: Wiktor Zychla | last post by:
I have a problem I cannot solve. My application hosts IE activex control. I follow the standard procedure: I just aximp shdocvw.dll. Note that this gives you two files: axshdocvw.dll and shdocvw.dll. And there comes the problem. I reference the libraries from my project. For this to work, I put the libraries in the same directory as the application. Now, from my code I try to ShellExecute to a link (to show it in a new window):
6
1444
by: ?scar Martins | last post by:
Hi When I'm debugging and somewhere in the code I have a breakpoint, many times when the code after breakpoint finishes and the app returns I can do nothing within in it(it's like freeze)... The app behaves strangely and not accepts any event even close event!!!!! It just lefts for me to stop debugging clicking the respective button in the vs.net toolbar... I don't know if this behaviour is a bug of vs.net or if it's consequence of bad...
10
3739
by: MLH | last post by:
Gentlemen: I am having one heck of a time taking a DAO walk through the records in an SQL dynaset. I'm trying to walk a set of records returned by a UNION query. I'm attempting to filter the records to those related to vehicle #60 ( = 60 ). If I explicitly specify 60 in the SQL ==everything works fine. Take a look: 100 PString = "SELECT & " & Chr$(&H22) & Space(1) & Chr$(&H22) & " & AS Recipient " 120 PString = PString & "FROM...
2
9286
by: Viewer T. | last post by:
I am trying to write a script that deletes certain files based on certain criteria. What I am trying to do is to automate the process of deleting certain malware files that disguise themselves as system files and hidden files. When I use os.remove() after importing the os module is raises a Windows Error: Access denied probably because the files have disguised themselves as system files. Is there anway to get adequate privileges under...
11
3161
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global" storage is ButtonHasBeenClicked. In this simple example code in Form1 calls a routine in Module1 which then calls code back in Form1 (subroutine WhatEver). WhatEver needs to access ButtonHasBeenClicked but the reference to ButtonHasBeenClicked...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.