Results 1 to 5 of 5

Thread: Shell and wait... as well as a Kool way to find a Program associated with a filename

  1. #1

    Thread Starter
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Thumbs up Shell and wait... as well as a Kool way to find a Program associated with a filename

    How To Shell to Access and wait until it is closed
    then Update a forms records when Access is closed (and
    therefore updated)..

    The shell and wait part of this solution will also work for any
    other program. As will Get_Program_Path. These use the API.

    The answer tho simple in itself didn't address the need to tell
    when The Access DB was closed (and therefore updated)..

    I had to implement the following which is very interesting because
    it also shows how to wait while an app is open until it is closed..

    I tried ShellExecute with no joy and had to use Shell instead..


    VB Code:
    1. 'Note
    2. 'I am pasting this in in the order in which it is called
    3. ' by the menu item
    4.  
    5.  
    6.  
    7. 'dimmed in a module
    8. Private Declare Function FindExecutable Lib "shell32.dll" Alias _
    9. "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, _
    10. ByVal lpResult As String) As Long
    11.  
    12.  
    13. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess _
    14. As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    15.  
    16. Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle _
    17. As Long, ByVal dwMilliseconds As Long) As Long
    18.  
    19. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    20.  
    21.  
    22. Private Sub mnuOpenDB_Click()
    23.  Dim StrAppName As String, StrFileName As String
    24.  Screen.MousePointer = vbHourglass
    25.  ' Hide.
    26.     Me.Visible = False
    27.     'Do other stuff
    28.     DoEvents
    29.     StrFileName = App.path & "\" & "OrdOut.mdb"
    30.    
    31.     'Find the path to the Access Executable
    32.     'and build the command string to open the Database
    33.        StrAppName = Get_Program_Path(StrFileName) & _
    34.         " " & StrFileName
    35.        
    36.         'Run The App and wait till it is closed
    37.         ShellAndWait StrAppName, vbNormalFocus
    38.        
    39. 'refresh the controls
    40.  EmData.Refresh
    41.  EData.Refresh
    42.  LoadEmailListBox
    43.  loadFileList
    44.  LstFilesSaved.Refresh
    45.  LstEmailAdds.Refresh
    46.  
    47.     ' Reappear.
    48.     Me.Visible = True
    49.    
    50. Screen.MousePointer = vbDefault
    51.  
    52. End Sub
    53.  
    54. Public Function Get_Program_Path(StrPassedFName) As String
    55. Dim sAccess As String, StrFileName As String
    56.  
    57. StrFileName = StrPassedFName
    58.  
    59. 'This could be your File String too
    60. ''App.path & "\" & "OrdOut.mdb"
    61.  
    62. ' Use the "FindExecutable" API to return the path
    63. 'To the App associated with the filename.
    64.   sAccess = Space(260)
    65.   Call FindExecutable(StrFileName, "", ByVal sAccess)
    66.   sAccess = Replace(Trim(sAccess), Chr(0), "")
    67.     If Len(sAccess) <> 0 Then
    68.         'MsgBox sAccess, vbInformation, "The Path to Access is"
    69.         GetAccess_Path = sAccess
    70.     End If
    71. End Function
    72.  
    73.  
    74. Public Sub ShellAndWait(ByVal program_name As String, ByVal window_style As VbAppWinStyle)
    75. Dim process_id As Long
    76. Dim process_handle As Long
    77. Dim lngdb  As Long
    78.  
    79.     ' Start the program.
    80.     On Error GoTo ShellError
    81.     process_id = Shell(program_name, window_style)
    82.     'process_id = ShellExecute(Me.hwnd, "open", program_name, vbNullString, App.path, 1)
    83.  
    84.     On Error GoTo 0
    85.  
    86.  
    87.  
    88.     ' Wait for the program to finish.
    89.     ' Get the process handle.
    90.     process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
    91.     If process_handle <> 0 Then
    92.         WaitForSingleObject process_handle, INFINITE
    93.         CloseHandle process_handle
    94.     End If
    95.  
    96. Exit Sub
    97. ShellError:
    98.     MsgBox "Error starting task " & _
    99.         program_name & vbCrLf & _
    100.         Err.Description, vbOKOnly Or vbExclamation, _
    101.         "Error"
    102. End Sub







    bindu

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    I love the Shell and Wait function, just make sure you define:

    Private Const SYNCHRONIZE As Long = &H100000
    Private Const INFINITE As Long = &HFFFFFFFF

  3. #3
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    I haven't tried your code but don't see how it could work as
    this function return is wrong (see <<) as not the same as the function name.

    VB Code:
    1. Public Function Get_Program_Path(StrPassedFName) As String
    2. Dim sAccess As String, StrFileName As String
    3.  
    4. StrFileName = StrPassedFName
    5.  
    6. 'This could be your File String too
    7. ''App.path & "\" & "OrdOut.mdb"
    8.  
    9. ' Use the "FindExecutable" API to return the path
    10. 'To the App associated with the filename.
    11.   sAccess = Space(260)
    12.   Call FindExecutable(StrFileName, "", ByVal sAccess)
    13.   sAccess = Replace(Trim(sAccess), Chr(0), "")
    14.     If Len(sAccess) <> 0 Then
    15.         'MsgBox sAccess, vbInformation, "The Path to Access is"
    16.         GetAccess_Path = sAccess        '<<<<<<<<<<<<<<<<<<<
    17.     End If
    18. End Function

  4. #4

    Thread Starter
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    mm

    so change it


  5. #5
    New Member
    Join Date
    Jan 2004
    Posts
    3

    Tanx!

    Thank you!

Posting Permissions

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



Click Here to Expand Forum to Full Width