Click to See Complete Forum and Search --> : Newbie question


sdowdall
March 20th, 2001, 12:33 PM
Hi,
I have a combo box that has to application names as choices. I want the user to click a button and when that happens, it reads what is currently displayed in the combo box and launches that application. The path would be something like c:\bsc\bsc.exe to launch the selected application.

So how do i read in the selected values of a combo box and then launch an application (based on the selected value) with a button click?

Any examples would be great...I'm just starting.

Thanks,
Steve

epelyavski
March 20th, 2001, 12:46 PM
Try something like this...


private Sub cmdRun_Click()
If cmbAppl.ListIndex >= 0 then
Shell cmbAppl.List(cmbAppl.ListIndex)
else
MsgBox "Please select the application"
End If
End Sub

Raistlin
March 20th, 2001, 12:48 PM
Here is the exact code.
I used it before so it works.
Say the combo box is named cboApp

dim AppName as string
AppName = cboApp.text



Here is the function:(Just send the path and filename to the function)

Function ShellAndWait(FileName as string)
Dim objScript
Dim ShellApp
on error GoTo ERR_OpenForEdit
set objScript = CreateObject("WScript.Shell")
' true for the Wait.
ShellApp = objScript.Run(FileName, 1, true)
ShellAndWait = true
EXIT_OpenForEdit:
Exit Function
ERR_OpenForEdit:
MsgBox Err.Description
GoTo EXIT_OpenForEdit
End Function




If this function doesn't work replace the
ShellApp = objScript.Run(FileName, 1, True)
line with SHELL(filename)

The shell basically create a DOS session and gives it the command you send it.

If you have any question e-mail me.
Raistlin
Please vote if this is any help

sdowdall
March 20th, 2001, 01:01 PM
Wow, you guys are quick. I guess i'm starting to get it, you create a shell and feed it the .exe name.
What is cmbAppl? Is that the name of the combobox? Sorry, but i really am starting out.

also, how do i specify two differnet application paths? Remember, if they choose one item in the combobox then it goes to one application, if they choose the second then it goes to a different app.

Thanks,

Steve

Raistlin
March 20th, 2001, 01:13 PM
The cmbApp1 is a combo box.
And what I would do is have hidden variable

Dim strApp as string 'declare as global(at the top)
public Sub cmbApp1_change() 'The change event for the combo box
Select Case cmbApp1.text
Case "APPLICATIONCHOICE1":
strApp = "C:\pathtoapp\appname1.exe"
Case "APPLICATIONCHOICE2":
strApp = "C:\pathtoapp\appname2.exe"
End Select
End Sub





This should do it. Now when they select the App in the combobox the change event will set the
global variable strApp to the correct path and you send that to the shell function.


Raisltin

sdowdall
March 20th, 2001, 01:19 PM
awesome! now i guess the last piece is how do i make it work from a button click?

Steve

Palin00
March 20th, 2001, 01:34 PM
Just create a button and on the click event
execute the shell function


public Sub cmdbutton_click
Call Shell_Function(strAPP) 'That is it!!
End Sub




Just pick a shell function and cut and paste it into your project and create a button next to the combo box and uses the above code.

sdowdall
March 20th, 2001, 02:10 PM
I keep getting a label undefined error on the first line of the function. Did I forget something?

Code:

Dim strApp As String

Public Sub cmbApp_change() 'The change event for the combo box
Select Case cmbApp.Text
Case "SD Development": strApp = "C:\Winnt\notepad.exe"

Case "SD Production": strApp = "C:\Winnt\notepad.exe"

End Select
End Sub



Private Sub Command1_Click()
Call ShellAndWait(strApp)
End Sub

Private Sub Form_Load()

cmbApp.AddItem "SD Development"
cmbApp.AddItem "SD Production"

End Sub



Function ShellAndWait(FileName As String)

Dim objScript
Dim ShellApp

On Error GoTo ERR_OpenForEdit

Set objScript = CreateObject("WScript.Shell")
' true for the Wait.
ShellApp = objScript.Run(FileName, 1, True)
ShellAndWait = trueEXIT_OpenForEdit:
Exit Function
ERR_OpenForEdit: MsgBox Err.Description
GoTo EXIT_OpenForEdit
End Function

Raistlin
March 20th, 2001, 02:20 PM
Try this function:


Function ShellAndWait(FileName as string)

Dim objScript
Dim ShellApp

on error GoTo ERR_OpenForEdit

set objScript = CreateObject("WScript.Shell")
' true for the Wait.
ShellApp = objScript.Run(FileName, 1, true)
ShellAndWait = true
Exit Function
ERR_OpenForEdit:
MsgBox Err.Description
End Function



Raistlin

sdowdall
March 20th, 2001, 02:34 PM
say's invalid procedure call.

here is all my code.
----------------------------------
Dim strApp As String

Public Sub cmbApp_change() 'The change event for the combo box
Select Case cmbApp.Text
Case "SD Development": strApp = "C:\Winnt\notepad.exe"

Case "SD Production": strApp = "C:\Winnt\notepad.exe"

End Select
End Sub



Private Sub Command1_Click()
Call ShellAndWait(strApp)
End Sub

Private Sub Form_Load()

cmbApp.AddItem "SD Development"
cmbApp.AddItem "SD Production"

End Sub



Function ShellAndWait(FileName As String)

Dim objScript
Dim ShellApp

On Error GoTo ERR_OpenForEdit

Set objScript = CreateObject("WScript.Shell")
' true for the Wait.
ShellApp = objScript.Run(FileName, 1, True)
ShellAndWait = True

Exit Function
ERR_OpenForEdit: MsgBox Err.Description

End Function
----------------------------------
thanks,

steve

Raistlin
March 21st, 2001, 11:51 AM
I have got it.
Here is the code Just Copy and Paste it into your project. The reason why it wasn't working is
because the change event did not fire properly. Since you have a command button just do this:




Dim strApp as string




private Sub Command1_Click()
Select Case cmbapp.Text
Case "SD Development": strApp = "C:\Winnt\notepad.exe"

Case "SD Production": strApp = "C:\Winnt\notepad.exe"

End Select
Call ShellAndWait(strApp)
End Sub

private Sub Form_Load()

cmbapp.AddItem "SD Development"
cmbapp.AddItem "SD Production"

End Sub



Function ShellAndWait(FileName as string)

Dim objScript
Dim ShellApp



set objScript = CreateObject("WScript.Shell")
' true for the Wait.
ShellApp = objScript.Run(FileName, 1, true)
ShellAndWait = true




End Function





Don't forget to vote.
Thank You
If you have any more question feel free to ask.
Raisltin

sdowdall
March 21st, 2001, 04:04 PM
You done real good! I'm impressed.
Thanks so much for you help.

One problem. It does not seem to run an application if the path is to another directory like c:\Program Files\Myapp\myapp.exe.

any thoughts?

Steve

Raistlin
March 21st, 2001, 04:27 PM
That I am confused on.
It should.
I tried the app on my machine with a different program and it worked.
make sure the path is correct by opening a DOS session and type the exact path and filename that you have in your code. If it works then there should be no reason that the app shouldn't work

Good luck and thanks for your votes.
Raistlin

sdowdall
March 21st, 2001, 07:34 PM
Got it. I have to use the DOS equivilant name for Program Files (PROGRA~1) for it to work. It's workin great. Many thanks to all who helped me.

Steve