Reusing Application object

SunWuKung

Registered User.
Local time
Today, 08:22
Joined
Jun 21, 2001
Messages
172
I am using objects from a statistical package from Access VBA.

The problem is that each time the code runs I need to create a new Application object (start a new instance of the statistical program) with Set StatApp= New xxx.Application and starting the application takes a relatively long time.

How could I open the application only once and than refer to the open application in subsequent runs (once the original sub stopped)?

Thanks for the help.
SWK
 
Try making the object variable Public and set it on the opening of the database, and set it to Nothing when the database closes.
 
I tried this this but this way the object only remains defined while the code is running. After the code stops - when I run the code again the application is open but the object's value is nothing.

I put this code in a module and call the sub from the immediate window.

Option Compare Database
Option Explicit
Public StatApp As STATISTICA.Application

Sub Main()
On Error GoTo Error_
Dim StatDataSpreadsheet, StatResultSpreadsheet As Spreadsheet
Dim StatQueries As Queries
Dim StatQuery As Query
Dim StatAnalysis As Analysis
Dim As Spreadsheet
Dim Msg As String

Set StatApp = New STATISTICA.Application

When I call this the second time, before the new application line StatApp is undefined, after that I get a new instance of the application.

Do you have any idea how could I reuse the application that is already open?
 
Sun,

If your form is closed, the object reference is killed. If you want the object to be available as long as the app is open (without having to re-reference it), consider putting the code that creates the object variable into a stand-alone module.

Code:
Option Explicit

	Public StatApp As STATISTICA.Application
'etc.

Then create a hidden start form for your Access app -- use it's load event to initialize stuff before it opens your user's start form.

Code:
'Create an object reference.
	Set StatApp = New STATISTICA.Application 
'etc.

	DoCmd.Open MyUserStartForm...

Leaving the hidden form open until your app closes allows you to re-use the object throughout the rest of your app without re-setting the reference.

Code:
'Calculate the square root of a pair of shoes...
	Dim MyVar
	MyVar = statistca.SQRootShoesMethod

Regards,
Tim
 
I did put the object creation in a public variable in a standalone module but I didn't no that I need to keep a form open in order to reference the public variable - this is good to know.

I tried an other way meanwhile and it seems to work for me.
Instead of keeping the reference I check if I have the application open and if I do I take it, if I don't I open it:

Dim StatApp As STATISTICA.Application
On Error Resume Next
Set StatApp = GetObject(, "STATISTICA.Application")
On Error GoTo Error_
If StatApp Is Nothing Then Set StatApp = New STATISTICA.Application

Thanks for the help.
SWK
 

Users who are viewing this thread

Back
Top Bottom