[AccessD] Shelling to a batch file

Max Wanadoo max.wanadoo at gmail.com
Thu Mar 4 17:35:55 CST 2010


Thanks Stuart,

That put me  on the right track.

Cheers

Max
 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Stuart McLachlan
Sent: Thursday, March 04, 2010 10:54 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Shelling to a batch file

Several problems.

You are not currently using the SHELL command, you are using ShellExecute
which is Windows API call.  Any reason for that?

You are ShellExecuting a string that looks like "CD /D C;\MyAppDir"

CD is an internal DOS command.  You can't run it directly , you  need to
invoke a command shell. Something along the lines of

sPath =  conQuote &  "C:\Windows\System32\CMD.exe", " /C CD /D " _
   & CurrentProject.Path &  conQuote.

Note that in this example, the path to CMD.exe is hard coded.  To be
portable, you actually need to determine the location of CMD.Exe on the
workstation and embed this in the string.  I have code to do it somewhere,
I'll try to dig it out later.

But that still won't do what you want.  What happens here is that you are
invoking a command shell and changing directory in it.  You then close that
shell and invole a second one to run Blat.  That second shell will again run
in your working directory, not your application directory.

A couple of alternatives:

1.  Include the CD command as the first line in Blat.bat   You just need to
build it once when 
the application is installed, or it may be preferable to build Blat.bat with
a CD to currentproject.path and save it immediately before shelling to it.

2.  Change your applications working directory in VBA immediately before and
after calling Blat.bat.  Here's a simple example of doing so:

Function test()
Dim strDirStore As String
strDirStore = CurDir
Debug.Print CurDir
ChDir CurrentProject.Path
Debug.Print CurDir
ChDir strDirStore
Debug.Print CurDir
End Function





On 4 Mar 2010 at 21:35, Max Wanadoo wrote:

> Hi All,
> 
> I am struggling to get the SHELL command to run a batch file from Access.
> 
> Any clues  or tips?
> 
> The batch file in turn will run and executable in the same folder as 
> the currentproject.path BUT this exe file is NOT installed so it is 
> necessary to ensure that I first move to that folder and then run the
batch.bat file.
> 
> Eg:
> g\_MyTest is my currentproject.path within Access In there I have 
> blat.exe which is not installed and which I do not want to install - 
> so nothing in the Registry.
> I also have blat.bat which I have created from Access.
> I want to run the batch file which in turn will invoke the blat.exe 
> and pass it parameters.
> 
> The batch file runs fine if manually invoked.  This is the last bit of 
> running Blat from within Access.
> I will then post the lot to the List.
> 
> Thanks
> 
> Max
> 
> This is where I am at:
> 
> Private Sub sShell(sFile)
> 	' sfile will be "blat.bat"
> 	const conQuote as string = """"
>     Dim sPath As String
>     ' move into the current folder because we havn't installed Blat.exe
>     sPath = conQuote & " CD /D " & CurrentProject.Path & conQuote
>     Debug.Print sPath
>     ShellExecute Application.hWndAccessApp, "Open", sPath, "", "", 
> vbNormalFocus
> 
>     ' now run the batch file
>     'sFile = conQuote & sFile & conQuote
>     Debug.Print sFile
>     ShellExecute Application.hWndAccessApp, "Open", sFile, "", "", 
> vbNormalFocus End Sub
> 
> 
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list