Close one mdb open another

  • Thread starter Thread starter Arturo
  • Start date Start date
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.
 
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
 
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.
 
Back
Top