Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell a non .exe and wait

Status
Not open for further replies.

bmoyer23

MIS
Dec 5, 2001
46
US
How do I open an access application (.mde) from VB and wait until it finishes. Keep in mind I do not want to pass the install path of MS Access. The code below, which came from the FAQ section, works only with .EXE files. Please help. Thanks in advance.

Public Function ExecCmd(cmdline$)

Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
ret = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)

ExecCmd = ret&

End Function
 
First part of your question:

For example, the following command line starts Microsoft Access and opens the Northwind sample database for exclusive access.

"C:\Program Files\Microsoft Office\Office10\MSAccess.exe" "C:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb" /excl
Use this in conjunction with the shell function to start Access from VB. Keep in mind, though, that this will just start Access with the specified DB and options. It will not keep you from any other other process while Access is running.

If you want to shell out andf wait for the application to complete before returning to VB, this should help you.

Run Shelled App and Wait Until Finished
faq222-428


Option Explicit

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&
'-----------------------------------------------------------
Sub Main()
Dim sPrgm As String

sPrgm = "C:\Winnt\notepad.exe"

RunUntilFinished sPrgm

MsgBox "App is finished!"

End
End Sub
'-----------------------------------------------------------
Public Sub RunUntilFinished(ByVal sApp As String)
Dim lProcID As Long
Dim hProc As Long

' Start the App
On Error GoTo ErrHndlr
lProcID = Shell(sApp, vbNormalFocus)
On Error GoTo 0

DoEvents

' Wait for the App
hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
If hProc <> 0 Then
WaitForSingleObject hProc, INFINITE
CloseHandle hProc
End If
Exit Sub

ErrHndlr:
MsgBox &quot;Error starting App:&quot; & vbCrLf & _
&quot;App: &quot; & sApp & vbCrLf & _
&quot;Err Desc: &quot; & Err.Description
Err.Clear
End Sub Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
Thanks for the reply Bob.

I was looking for a way to lauch an Access application without providing the install location (ie: c:\program files\ms office\...). I tried using the RunUntilFinished function with the ShellExecute api, but it did not work.

Any other suggestions.
 
I think what you are asking for is equivalent to starting WORD from VB by providing a name ending in a .doc suffix, and that would automatically start WORD for you

I think you must provide a command string passed to a shell function. Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top