By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,122 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,122 IT Pros & Developers. It's quick & easy.

How to Automate the Running of Multiple Applications

100+
P: 119
I have a number of models that I need to run at regular times each week. The models may be based in Access, Excel or a math based statistics package called R. The models may also be a combination of all three. For example, Access may pull in historical pricing information, this is processed by R, and the results are displayed in Excel.

I can obviously use the Windows "Scheduled Tasks" application to kick the process off at the required time, but I will also need a way of keeping track of each step of a process. For example, if I had the following sequence:

1. Pull data into Access
2. Run R script (via .bat file)
3. Store R data in database
4. Open Excel to plot data

I could potentially achieve 1-3 from VBA in Access, but to run step 4 I would need to know when R had finished so that Excel would not load too early. Similarly, if I start Access from a .bat file (or some other location), then I would need to know when Access had finished pulling in data before starting R, and again when R had finished before starting Excel.

Does anyone know how I can achieve this kind of scheduling? I've thought of posting boolean values to a database table giving an indication of whether or not an application has finished, but that seems like it could be troublesome. I'm not sure what else I could do.

I appreciate this is quite a general question, but I'd be interested in knowing if anyone has had this kind of problem before, or could suggest a solution.
Feb 20 '09 #1
Share this Question
Share on Google+
8 Replies


ADezii
Expert 5K+
P: 8,623
You can Shell Out and run multiple, independent, processes and wait for each one to finish before you execute the next in sequence, via a little API trickery:
  1. Copy and Paste the following API Declarations and Constants into a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    2. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    3. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    4.  
    5. Private Const SYNCHRONIZE = &H100000
    6. Private Const INFINITE = -1&
  2. Copy and Paste the following Sub-Routine Procedure into the same Standard Code Module as the Declarations and Constants:
    Expand|Select|Wrap|Line Numbers
    1. Public Sub RunUntilFinished(ByVal sApp As String)
    2. Dim lProcID As Long
    3. Dim hProc As Long
    4.  
    5. 'Start the App
    6. On Error GoTo ErrHandler
    7.  
    8. 'Retrieve the Ptrocess ID
    9. lProcID = Shell(sApp, vbNormalFocus)
    10.  
    11. On Error GoTo 0
    12.  
    13. DoEvents
    14.  
    15. 'Wait indefinately for the App
    16. hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
    17.  
    18. If hProc <> 0 Then
    19.   WaitForSingleObject hProc, INFINITE
    20.   CloseHandle hProc
    21. End If
    22.  
    23. Exit_Routine:
    24.   Exit Sub
    25.  
    26. ErrHandler:
    27.   MsgBox "Error starting " & sApp & vbCrLf & vbCrLf & Err.Description, _
    28.           vbExclamation, "Error in RunUntilFinished()"
    29.   Err.Clear
    30.     Resume Exit_Routine
    31. End Sub
  3. Copy and Paste the following code segment to anywhere appropriate:
    Expand|Select|Wrap|Line Numbers
    1. 'Run Windows Calculator
    2. Call RunUntilFinished("C:\Windows\System32\Calc.exe")
    3.  
    4. 'Notepad will not start until Calculator is closed
    5. Call RunUntilFinished("C:\Windows\System32\Notepad")
    6.  
    7. 'Paint will not start until Notepad is closed
    8. Call RunUntilFinished("C:\Windows\System32\MSPaint.exe")
  4. Now that you did all that, just download the Attachment, silly (LOL)!
Feb 21 '09 #2

100+
P: 119
Awesome...This looks fantastic. I'll give it a try on Monday and let you know how it goes.

Thanks for such a quick response.
Feb 21 '09 #3

ADezii
Expert 5K+
P: 8,623
@billelev
You are quite welcome, let me know how you make out.
Feb 21 '09 #4

NeoPa
Expert Mod 15k+
P: 31,419
Check out the ShellWait() Function for a way to handle this in a basic way. This doesn't mean there's anything wrong with ADezii's code (at all). Sometimes though, it's easier to keep the esoteric, O/S, stuff in a separate module. Develop it then just use it as designed.
Feb 22 '09 #5

100+
P: 119
ADezii, I have tried your code and it works really well. Many thanks for your input.

I have one other issue, *** Edit *** which can now be found in a new thread.
Feb 23 '09 #6

NeoPa
Expert Mod 15k+
P: 31,419
I have moved the question part of this to another thread (Access Command Line Arguments).

This is for all the usual reasons that asking multiple questions in the same thread is not a good idea. Principal of which is that it gets much better exposure.
Feb 23 '09 #7

FishVal
Expert 2.5K+
P: 2,653
Command line arguments are not the only and, IMHO, not the most suitable way to trigger some specific action of Access application when opening it via VBA code in other Access application.

Did you consider using application automation?
Feb 23 '09 #8

100+
P: 119
I have...But only in passing. I'm playing around with the Shell method and then might resort to application automation at a later point. I'm trying to come up with a framework that will work with multiple applications, not just Access, and so Shell might be better in that instance.

In any case, thank you everyone for your help with my automation questions.
Feb 23 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.