Send to VB app parameters from command line

Posted on 2005-05-01
Last Modified: 2008-02-01
I need to create a VB app which receives external parameters from command line and do something. I'll like to know if somebody has some basic code to do it.

For example:

c:\>vbapp.exe hello 2

and in VB for example some like:

for i=0 to param2
  alert param1

I hope my idea is clear.

Basically i need this to integrate one existing app in powerbuilder to a new one i must create so powerbuilder sends data to vb app and do something

Question by:cavagnaro
    LVL 5

    Assisted Solution

    Maybe you can use this solution.
    In the main form load, or sub main or whatever first sub that you call first put this code :

      Dim strargs() As String
      dim param1 as string
      dim param2 as integer
      If Command() = "" Then Exit Sub    'command() holds all of the arguments given to the executable
        strargs() = Split(Command(), " ")  
        param1 = strargs(0)
        param2 = cint(strargs(1))

      for i=0 to param2
        alert param1

    Hope that help.

    LVL 14

    Assisted Solution

    by:Shiju Sasidharan
    'Make Exe of ur Project and try like this in command prompt
    '   C:\Project1.exe arg1 arg2 arg3
    Private Sub Form_Load()
     Dim args() As String
     Dim i As Integer
        If Command = "" Then
           MsgBox "Arguments Empty"
           Exit Sub
        End If
        args = Split(Command, " ")
        For i = 0 To UBound(args)
          MsgBox args(i)
        Erase args
    End Sub

    LVL 12

    Accepted Solution

    > c:\>vbapp.exe hello 2
    > and in VB for example some like:
    > for i=0 to param2
    >   alert param1
    Option Explicit
    'compile this project into an exe and run with the parameters.....
    'c:\>vbapp.exe hello, 2

    add this code to your form.........

    Private Sub Form_Load()
       Dim x As Variant
       Dim cmdArray As Variant
       x = Command()              'x will hold the command line variables
       cmdArray = Split(x, ",")
       'now cmdArray(0) will contain the value "hello"
       ' and cmdArray(1) will contain the value 2
       ' do someting with the passed in values.

    End Sub

    LVL 32

    Assisted Solution

    by:Robberbaron (robr)
    If you want it to run properly without showing a form when called from another program,
    set the Project Property to 'Run Unattended' and dont add any forms. (it wont let you i think...)

    Create a module with Sub Main in it, then use any of the first 2 code samples above to get paramaters. (sample 3 splits on a comma, which is not usual on a command line. )

    Note the code above requires VB6 for the Split command.
    There are lots of routines around to do the same in VB5. let me know if u need one.
    LVL 2

    Author Comment

    Last 3 answers are fully code operative and very helpful. Thanks

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now