Using the Command Function to Pass Arguments to an Application - December 2001

A common news group question goes something like:

How can I pass arguments to my application?

The Command function retrieves information from the command line. It makes the information available to your application. I have seen the question posted so many times in news groups that I decided to write a demo program and show how to use the function. So, here it is, the tip-of-the-month.

Command Function - From VBs Help File

Returns the argument portion of the command line used to launch Microsoft Visual Basic or an executable program developed with Visual Basic.

Syntax

Command

Remarks

When Visual Basic is launched from the command line, any portion of the command line that follows /cmd is passed to the program as the command-line argument. In the following example, cmdlineargs represents the argument information returned by the Command function.

VB /cmd cmdlineargs

For applications developed with Visual Basic and compiled to an .exe file, Command returns any arguments that appear after the name of the application on the command line. For example:

MyApp cmdlineargs

The Demo

I wrote a demo program to demonstrate the use of the function. A screen shot of the VB6 demo:

In this example the command line contains three arguments. The arguments are parsed from the command line and displayed in the text boxes. This sample was run from the VB IDE. The command information was entered via the project properties 'make' dialog, for example:


A modified Make dialog - see Make VB's Dialog Boxes Larger.

 

Compiled Example

I compiled the 'application' [included in the zip file] and ran it from a DOS console. Arguments are entered on the command line, for example:

The result:

The Demo Program Uses This Module

Module - Click for More Info Description Used in the Demo To
modConnectToWebSite.bas Connect to a web site Show this info when Help/Info is
selected from the demo.

Download VB 6 Source Code

Click here to download the VB 6 source code [13k]

Previous Tip   Tip Index   Next Tip    First Tip

Home

Updated 2001/12/20