Close one mdb open another

A

Arturo

I used to use a script to close one Access DB and then
open another. The script is as follows:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'MSAccess.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
Set objShell = CreateObject("WScript.Shell")
objShell.Run "MSAccess.exe C:\Folder 1\File 1.mdb"

I do have spaces in the names but it never mattered before.
I run it and keep getting the message that an incorrect command
was used to open access. Anybody know what is wrong or do you
have another script?

Thank you.
 
G

Graham Mandeno

Hi Arturo

Yes, the spaces are what is causing the problem. If your file or folder
names have spaces then the whole filename needs to be enclosed in quotes.
Two consecutive quotes inside a literal string in VBA or VBScript are
interpreted as one quote, so your Shell command should be:

"MSAccess.exe ""C:\Folder 1\File 1.mdb"""

Is this VBA code running from inside the database you are closing? If so
then using WMIService and WScript is way over the top. All you should need
is:

Shell "MSAccess.exe ""C:\Folder 1\File 1.mdb""", vbMaximizedFocus
Application.Quit
 
A

Arturo

That worked great. I called the script from within Access.

Thank you.

Graham Mandeno said:
Hi Arturo

Yes, the spaces are what is causing the problem. If your file or folder
names have spaces then the whole filename needs to be enclosed in quotes.
Two consecutive quotes inside a literal string in VBA or VBScript are
interpreted as one quote, so your Shell command should be:

"MSAccess.exe ""C:\Folder 1\File 1.mdb"""

Is this VBA code running from inside the database you are closing? If so
then using WMIService and WScript is way over the top. All you should need
is:

Shell "MSAccess.exe ""C:\Folder 1\File 1.mdb""", vbMaximizedFocus
Application.Quit
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand


Arturo said:
I used to use a script to close one Access DB and then
open another. The script is as follows:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'MSAccess.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
Set objShell = CreateObject("WScript.Shell")
objShell.Run "MSAccess.exe C:\Folder 1\File 1.mdb"

I do have spaces in the names but it never mattered before.
I run it and keep getting the message that an incorrect command
was used to open access. Anybody know what is wrong or do you
have another script?

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top