Command Function

Description

You can use the Command function to return the argument portion of the command line used to launch Microsoft Access.

Syntax

Command

Remarks

When Microsoft Access is launched from the command line, any portion of the command line that follows the /cmd option is passed to the program as the command-line argument. You can use the Command function to return the argument that has been passed.

To change a command-line argument once a database has been opened, click Options on the Tools menu. On the Advanced tab of the Options dialog box, enter a new argument in the Command Line Arguments box. The Command function will now return the new argument you have entered.

When the Command function is used anywhere other than in Visual Basic code in a module, you must include empty parentheses after the function. For example, to use the Command function in a text box on a form, you would set the ControlSource property of the text box to an expression like the following:

=Command()
Example

The following example shows how to launch Microsoft Access with a command-line argument, then shows how to return the value of this argument by using the Command function.

To test this example, click the Windows Start button and click Run. Type the following in the Run dialog box, on a single line. (You must surround the parts of the command line information in quotation marks).

"C:\Program Files\Microsoft Office\Office\Msaccess.exe" _
    "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb" _
    /cmd "Orders"
Next, open a new module in the Northwind sample database and add the following function.

Function CheckCommandLine()
    ' Check value returned by Command function.
    If Command = "Orders" Then
        DoCmd.OpenForm "Orders"
    ElseIf Command = "Employees" Then
        DoCmd.OpenForm "Employees"
    Else
        Exit Function
    End If
End Function
When you call this function, Microsoft Access will open the Orders form.

You can create a macro named AutoExec to call this function automatically when the database is opened.