Command Function 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. For more information on carrying out an action when a database opens, see Carry out an action when my database first opens.