Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

telling if MS Access is running 2

Status
Not open for further replies.

clientuser

Programmer
Apr 18, 2001
296
US
I need to write a small app that tells if MS Access in running on the same machine and if it is, to kill the process and shut it down..

any suggestions?
 
Go to and search for "getobject createobject", for All Words and this is #2.

It uses "Excel.Application". I do not know what it is for Access. Read the KB article for info on various versions etc.


GetObject
GetObject is used to attach to a running instance of an automation server. There are a few different ways to call GetObject, but the syntax that is recommended for the Microsoft Office applications is as follows:
set xlApp = GetObject(, "Excel.Application")
If an instance of Microsoft Excel is running when this code is executed, you have access to the running instance's object model through the xlApp variable. If no instance is running, you receive the following trappable run-time error message:
Run-time error '429':
ActiveX component can't create object
If multiple instances of Microsoft Excel are running, GetObject attaches to the instance that is launched first. If you then close the first instance, another call to GetObject attaches to the second instance that was launched, and so forth.

You can attach to a specific instance if you know the name of an open document in that instance. For example, if an instance of Excel is running with an open workbook named Book2, the following code attaches successfully to that instance even if it is not the earliest instance that was launched:
Set xlApp = GetObject("Book2").Application
 
Add a reference in your project to Microsoft Access Object Library. Then add the following procedure:

Public Sub KillAccess()
On error resume next
dim objAccess as Object
set objAccess = GetObject(, "Access.Application")
objAccess.Quit
set ObjAccess = Nothing
End Sub

That's the easiest way. You could also use some API functions to find an Access window (checking the caption of the title bar of Access ('Microsoft Access')).

Success

Herman
 
Hi John,

you're right, in this case you do not need a reference. However, if you want to program the Access object you get the IntelliSense advantage (autofinish variables and function names) when you make the reference.

Thanks, Herman
 
Is there a way to loop this statement so that it shuts down all open instances of Access?
 
There is a better way to do this ...

1) Enumerate the process list.
2) Use the terminateprocess API for each instance of Access Found - Jeff Marler B-)
 
I looked at the terminateprocess api, it seems that there should be a loop i can do with the code that i have already (the getobject code) that would take care of this?
 
Code:
Dim objApp As object
Dim lngErr as long
Do
   On error resume next 
       set objApp = GetObject(, "Access.Application") 
       lngErr = Err.Number
   On error goto 0
   if lngerr <> 0 then exit do
   On error Resume next
       objApp.Quit
       lngerr = Err.Number
   On error goto 0
   Set objApp = nothing
   if lngErr <> 0 then exit do ' Avoid Endless loop
Loop
 
Oh, I tried this sub, and it kept failing!! Ya know what was wrong with my VB app??? I had set Break on All Errors in the options, and, (duh!) it broke!, and the app crapped out yet again when it reached the sub!!..


Thank you all for your code and assistance, trying to find the answer to how to kill the hidden instance created from &quot;nowhere&quot; could be accomplished. (Where the heck is it instanciated anyway??)

Drew
 
jmarler, you suggested enumerating the processes; can I use this method to check if ANY process is running? If so, how?

I have a little starter app that kicks off WTS but I want to hide it when it's performed it's task until the WTS app is terminated, at which point the starter app needs to come back again to give the option of starting WTS again.

execcmd didn't work because once the shell had run the WTS app, the shell finished and so the starter could continue running.

cheers
Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top