How to Automate the Running of Multiple Applications

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billelev
    New Member
    • Nov 2006
    • 119

    How to Automate the Running of Multiple Applications

    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.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    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:
      Code:
      Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
      Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
      Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
      
      Private Const SYNCHRONIZE = &H100000
      Private Const INFINITE = -1&
    2. Copy and Paste the following Sub-Routine Procedure into the same Standard Code Module as the Declarations and Constants:
      Code:
      Public Sub RunUntilFinished(ByVal sApp As String)
      Dim lProcID As Long
      Dim hProc As Long
      
      'Start the App
      On Error GoTo ErrHandler
      
      'Retrieve the Ptrocess ID
      lProcID = Shell(sApp, vbNormalFocus)
      
      On Error GoTo 0
      
      DoEvents
      
      'Wait indefinately for the App
      hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
      
      If hProc <> 0 Then
        WaitForSingleObject hProc, INFINITE
        CloseHandle hProc
      End If
      
      Exit_Routine:
        Exit Sub
          
      ErrHandler:
        MsgBox "Error starting " & sApp & vbCrLf & vbCrLf & Err.Description, _
                vbExclamation, "Error in RunUntilFinished()"
        Err.Clear
          Resume Exit_Routine
      End Sub
    3. Copy and Paste the following code segment to anywhere appropriate:
      Code:
      'Run Windows Calculator
      Call RunUntilFinished("C:\Windows\System32\Calc.exe")
      
      'Notepad will not start until Calculator is closed
      Call RunUntilFinished("C:\Windows\System32\Notepad")
      
      'Paint will not start until Notepad is closed
      Call RunUntilFinished("C:\Windows\System32\MSPaint.exe")
    4. Now that you did all that, just download the Attachment, silly (LOL)!

    Comment

    • billelev
      New Member
      • Nov 2006
      • 119

      #3
      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.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by billelev
        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.
        You are quite welcome, let me know how you make out.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32648

          #5
          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.

          Comment

          • billelev
            New Member
            • Nov 2006
            • 119

            #6
            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.
            Last edited by NeoPa; Feb 23 '09, 08:38 PM. Reason: New question moved

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32648

              #7
              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.

              Comment

              • FishVal
                Recognized Expert Specialist
                • Jun 2007
                • 2656

                #8
                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?

                Comment

                • billelev
                  New Member
                  • Nov 2006
                  • 119

                  #9
                  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.

                  Comment

                  Working...