Link to home
Start Free TrialLog in
Avatar of mjwerner
mjwerner

asked on

Open .bat file

Hi
Is there a way I can open a batch file (.bat) using code or macros in an access db.
I have tried the RunApp action from a macro, but this flashes open the batch file and then it dissapears again without actually running.

Cheers
M
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try use ShellExecute API, like:

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long

Public Function StartDoc(ByVal FileName As String, Optional CommandLine As String = "") As Long
    Dim Scr_hDC As Long
    Scr_hDC = GetDesktopWindow()
    'change "Open" to "Explore" to bring up file explorer
    StartDoc = ShellExecute(Scr_hDC, "Open", FileName, CommandLine, vbNullString, 1)
End Function

Try like:

StartDoc "C:\test.bat
Avatar of mjwerner
mjwerner

ASKER

Hi ryancys
Have posted your code into a module and put the StartDoc "C:\test.bat" behind a form button and nothing seems to happen.
I have pasted your code exactly.
Cheers
M
try the code here: http://www.mvps.org/access/api/api0018.htm  apparently, (it's also based on ShellExecute, but the "glue logic" is different: all you hand it is a filename [-> your bat-file] and it should take care of the rest, so it might make a difference in your case).


--bluelizard
>>but this flashes open the batch file and then it dissapears again without actually running
As i remember for a DOS Prompt application (.bat), it will close itself automatically once it had been executed via code... ?

what about you trying to run a .txt file for testing, to see if the StartDoc function above works?

regards
<<...and then it dissapears again without actually running.>>

Put a pause statement in your .bat file.  I suspect that your batch file is running, but it may not be written to accomodate the environment.  For instance, if it expects to be in a certain directory or have certain files exist in the same directory, it will appear to begin/end quickly because there is nothing for it to do.
What is the lShowHow variable for? I have tried putting in a number as it is declared as long, eg:
fHandleFile ("C:\IMPEXP.BAT",500)
but I get an error:
Compile error:
Expected: =

In response to aikimark and ryancys:
The batch file runs fine on its own (by double clicking .bat file), it is designed to copy some files and output to a text file. This takes about 10 secs to process then closes automatically.
When I use the StarDoc function, the batch file opens for a fraction of a second then closes without performing its task.
Do you think putting in a pause statement will help this, if so how can I do this?

Cheers
M
Try to see if this helps?

http:Q_21316260.html
Shell "C:\Windows\cmd.exe /C C:\MyBatchFile.bat"

Change your path to cmd.exe accordingly.
PAUSE

Just add it to your .bat file.  The execution will pause.  You can add multiple PAUSE statements to allow you to step through your bat file.

What does your .bat file look like?
Are you expecting parameters?
In response to shanesuebsahakarn
Again screen just flickers, but batch file does not do its job.
What is the full path of the batch file that you are trying to execute? There is a limit of approximately 160 characters with all of these methods.
Opening .bat file using access function:
Using the PAUSE in the batch file, opens the .bat file, pauses, you then hit a key and the .bat window closes.

Opening .bat file manually:
Using the PAUSE in the batch file, opens the .bat file, pauses, you then hit a key and the .bat file performs it job and .bat window closes.

THE BAT FILE WORKS PERFECTLY ON ITS OWN, sorry for shouting, but there is nothing wrong with the batch file or its parameters that it calls from a .ini file.
I repeat:

> What is the full path of the batch file that you are trying to execute? There is a limit of approximately 160 characters with all of these methods.

If the full path to the file is more than 160 characters, it will not execute properly and you will see the behaviour that you describe.
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
M,

I'm not implying there is anything wrong with your .bat file.  It was probably working as design/written.  However, you are launching it from an application program and, thus, changing the environment in which it executes.
Have finally managed to sort it with by a mixture of things:
Using the following to call the batch file:
  Dim stAppName As String
  stAppName = "C:\test.bat"
  Call Shell(stAppName, 3)

and, yes I apologise, my batch file was partly to fault, I changed it from:
  R:\CIS\OLIB\Process\UserCSV.exe Sample.ini

to:
  R:
  cd\CIS\OLIB\Process
  UserCSV.exe Sample.ini

..and now it works, I'm not quite sure why, but it does, so cheers everyone.
<<..and now it works, I'm not quite sure why, but it does, so cheers everyone.>>

Glad I could solve your problem.

To help you better understand...
Let's say your MSAccess database resides in directory C:\mjwerner\OLIB.
When you are running your application, this is the current (default) directory.  When your code launched the batch file, the current directory hadn't changed.  As a result, your UserCSV.exe program was looking for the Sample.ini file in the current directory.  Not finding the Sample.ini, the program ended.

When you restructured your batch file with
R:
cd \CIS\OLIB\Process

you were setting the current drive and current directory within the environment of the batch file.  Thus the UserCSV.exe could find the Sample.ini file.
I think I most correctly diagnosed the core reasons why the questioner was observing the stated behavior and recommended the eventual solution to this problem.