Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Shell Function (Access 2002)

    I have done a search for Shell Function and Execute but don't see the answer to my specific question. When I am running the Shell Function it opens the application that I want to run but it does not stay open long enough to finish the execution of the program. Is there a way to get around this?

  2. #2
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Shell Function (Access 2002)

    You say it opens the application that I want to run but it does not stay open long enough to finish the execution of the program. What is the IT in it does not stay open long enough, the application you executed? ... to finish the execution of the program, What is the program, the application being opened or the access database?

    What application are you opening and what code are you using to drive it?

    If I understand your question, you are using the shell function to open an application and you then want to do something in the application manually before the access program continues. To do this, you could put a MSGBOX in your code after the shell execute. Thus, your MS Access program won't continue until you press the OK button in the MSGBOX. This is sort of a way to pause the program and allow you to work in the program.

    HTH
    Regards,

    Gary
    (It's been a while!)

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Shell Function (Access 2002)

    I am opening an *.exe - C++ Program. It doesn't finish executing the whole thing before it goes to the next line of the access code. I have a msgbox right after it but it still doesn't work. Perhaps there is no way to let it continue to run???

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Shell Function (Access 2002)

    You can use the Windows API functions CreateProcess and WaitForSingleObject. API functions are not for the faint-hearted, so stop reading here if you don't feel up to using them.

    See http://www.mentalis.org/apilist/CreateProcess.shtml for info on CreateProcess; it contains a link to an example named SuperShell that demonstrates how to use it together with WaitForSingleObject to let your code wait till a called program has finished executing. There are also two examples of these two API functions in this forum: <post#=32797>post 32797</post#> and <post#=65029>post 65029</post#>.

  5. #5
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Shell Function (Access 2002)

    Another API option (maybe a little simpler?) is the Windows API OpenProcess and GetExitCodeProcess functions together with VB Shell function. The OpenProcess function opens a handle to an existing process, while the GetExitCodeProcess function returns the exit code of a terminated process. Example of use in a standard code module:

    Option Compare Database
    Option Explicit

    Const PROCESS_QUERY_INFORMATION = &H400
    Const SYNCHRONIZE = &H100000
    Const STILL_ACTIVE = &H103&
    Const INFINITE = &HFFFFFFFF

    Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

    Private Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

    Public Sub apiRunAppThenWait(strCmdLine As String, intWinMode As Integer)
    On Error GoTo Err_Handler

    Dim hInstance As Long
    Dim hProcess As Long
    Dim lngRetval As Long
    Dim lngExitCode As Long

    ' Get Instance Handle & pass to OpenProcess function:
    hInstance = Shell(strCmdLine, intWinMode)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, _
    True, hInstance)

    Do
    lngRetval = GetExitCodeProcess(hProcess, lngExitCode)
    DoEvents
    Loop Until lngExitCode <> STILL_ACTIVE

    Exit_Sub:
    Exit Sub
    Err_Handler:
    Select Case Err.Number
    Case 53 ' File not found
    Beep
    MsgBox "Invalid command line - cannot open application.", _
    vbExclamation, "FILE NOT FOUND"
    Case Else
    Beep
    MsgBox "Error " & Err.Number & ": " & Err.Description
    End Select
    Resume Exit_Sub

    End Sub

    Simple example of use, opening Notepad:

    Sub TestRunApp()

    apiRunAppThenWait "NOTEPAD.exe", vbNormalFocus
    MsgBox "Application has finished running.", vbInformation, "DONE"

    End Sub

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •