Results 1 to 5 of 5

Thread: Problems executing command

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    9

    Problems executing command

    Hi there,

    I'm pretty new to VB6 and used to work with Access VBA, so please bear with me.
    At the moment I'm trying to have VB execute a command with some command line parameters. One of these parameters is taken from a textbox. For some reason the correct command is displayed in a msgbox, but when I try to have VB execute it, it doesn't do this correctly.
    This is my code:
    Code:
    Private Sub username_LostFocus()
    Dim sYourCommand As String
    sYourcommand0 = "C:\iris\encrypt.exe "
    sYourCommand1 = username
    sYourCommand2 = " > c:\iris\pwoutput1.txt"
    MsgBox (sYourcommand0 & sYourCommand1 & sYourCommand2)
    
        Dim retval As Double
        retval = Shell(sYourcommand0 & sYourCommand1 & sYourCommand2, vbNormalFocus)
    End Sub
    It seems that only sYourcommand0 is being executed and the rest is left behind. However as mentioned the messagebox does display the correct command while it seems to me that both should display identical.
    Any suggestions on how I can have VB execute this command are welcome.

    Also, in Access I used the AfterUpdate to perform an action once a textbox was updated, I cannot get this to work in VB6 and therefore used LostFocus. Would anyone have a solution for this?

    Kind regards,

    Ronald.

  2. #2
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Problems executing command

    the reason i suspect is the operation ceases after the execution of the first command. i think you can use the api waitforsingleobject to wait untill all the command is properly executed, or on the easy way
    you can try by creating a batch file and then executing with the shell command
    HTH

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    9

    Re: Problems executing command

    Thanks for your reply. In fact all 3 commands combined are one command. The first part is the executables. The 2nd and 3rd are the parameters to the executables.
    I would prefer not to use a batch file as the 2nd part of the command changed depending on what is entered into a textbox.

    I anyone could give me an example on how to execute a shell command with some parameters behind of which one is not static:

    Code:
    C:\iris\encrypt.exe username > c:\iris\pwoutput1.txt
    Above is the command I need executed where username is the input taken from a textfield called username.

    Cheers,

    Ronald.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Problems executing command

    Welcome to VBForums

    I'm not sure why you are using 3 separate variables (none of which are declared) rather than just one. Also the retval variable is pointless, as you don't actually use it after setting it.

    It would be better to write the code like this:
    Code:
    Private Sub username_LostFocus()
    Dim sYourCommand As String
      sYourCommand = "C:\iris\encrypt.exe " & username & " > c:\iris\pwoutput1.txt"
      MsgBox sYourCommand
    
      Shell sYourCommand, vbNormalFocus
    End Sub
    I doubt these changes will have any effect, but they make it easier to read and check.


    I suspect the issue is not related to your code as such, but to the command line you are using - I think you would get the same behaviour if you ran the same thing at a command prompt (in the same folder as your program).

    That may be due to the space after > , or it may be because the executable needs you to put the parameters inside quotes, or it may be because you haven't provided enough information (I don't know what username signifies, but if it is a filename you need the full path to the file).

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Problems executing command

    if any of the paths or username could have a space, it must be enclosed in quotes
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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