?
Solved

Open .bat file

Posted on 2005-03-03
20
Medium Priority
?
848 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:mjwerner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 3
  • +2
20 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 13447752
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
 

Author Comment

by:mjwerner
ID: 13447799
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
 
LVL 14

Expert Comment

by:bluelizard
ID: 13447975
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Ryan Chong
ID: 13448011
>>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
 
LVL 46

Expert Comment

by:aikimark
ID: 13448176
<<...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
 

Author Comment

by:mjwerner
ID: 13448188
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
 

Author Comment

by:mjwerner
ID: 13448228
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
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 13448263
Try to see if this helps?

http:Q_21316260.html
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 13448294
Shell "C:\Windows\cmd.exe /C C:\MyBatchFile.bat"

Change your path to cmd.exe accordingly.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13448413
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
 

Author Comment

by:mjwerner
ID: 13448427
In response to shanesuebsahakarn
Again screen just flickers, but batch file does not do its job.
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 13448436
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
 

Author Comment

by:mjwerner
ID: 13448472
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
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 13448618
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
 
LVL 46

Accepted Solution

by:
aikimark earned 300 total points
ID: 13448720
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
 
LVL 46

Expert Comment

by:aikimark
ID: 13448732
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
 

Author Comment

by:mjwerner
ID: 13450219
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
 
LVL 46

Expert Comment

by:aikimark
ID: 13450683
<<..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
 
LVL 46

Expert Comment

by:aikimark
ID: 14065604
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question