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
mjwernerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
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
0
mjwernerAuthor Commented:
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
0
bluelizardCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ryan ChongCommented:
>>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
0
aikimarkCommented:
<<...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.
0
mjwernerAuthor Commented:
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: =

0
mjwernerAuthor Commented:
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
0
Ryan ChongCommented:
Try to see if this helps?

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

Change your path to cmd.exe accordingly.
0
aikimarkCommented:
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?
0
mjwernerAuthor Commented:
In response to shanesuebsahakarn
Again screen just flickers, but batch file does not do its job.
0
shanesuebsahakarnCommented:
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.
0
mjwernerAuthor Commented:
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.
0
shanesuebsahakarnCommented:
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.
0
aikimarkCommented:
M,

Put multiple PAUSE statements in your batch file, both before and (more importantly) after the real-work statements.  What you are missing by not pausing after the file copy statements is the error messages from the copy operation.

Here's something for you to do:
1. Start | Run (on the desktop)
2. cmd (and Enter)
Note: this is will start a command prompt window
3. issue the appropriate commands to navigate drives and directories until the current directory is the one in which your MSAccess database resides
4. run the batch file using the same string you use from within your MSAccess event code.
Example:
C:\IMPEXP.BAT

5. look at the messages
============================
Something else to try:
copy the MSAccess database to the same directory (root?) as the .bat file.  Rerun your MSAccess project.

============================
Observation: This bat file resides in the root directory of your C: drive

Note: please re-read my original post in this discussion thread.  It is quite likely that your .bat file will run perfectly well when launched from the root directory but will not run when launched from some other directory.

Often, .bat files require either drive/path navigation commands or relative file addressing in order to work properly when launched from a variety of locations.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
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.
0
mjwernerAuthor Commented:
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.
0
aikimarkCommented:
<<..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.
0
aikimarkCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.