Applies ToAccess for Microsoft 365 Access 2024 Access 2021 Access 2019 Access 2016

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

Remarks

When 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:

  1. On the File menu, click Access Options.

  2. In the Application Settings dialog box, click Advanced.

  3. 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 for Applications (VBA) 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

Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

The following example shows how to launch Access with a command-line argument and 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 code in the Run box on a single line. (You must surround the parts of the command line information in quotation marks).

"C:\Program Files\Microsoft Office\Office11\Msaccess.exe" _
"C:\Program Files\Microsoft Office\Office11\Samples\Northwind.mdb" /cmd "Orders"

Next, create a new module in the Northwind Traders sample database and add the following Sub procedure:

Public Sub CheckCommandLine()
    ' Check the value returned by Command function and display
    ' the appropriate form.
    If Command = "Orders" Then
        DoCmd.OpenForm "Orders"
    ElseIf Command = "Employees" Then
        DoCmd.OpenForm "Employees"
    Else
        Exit Sub
    End If
End Sub

When you call this procedure, Access opens the Orders form. You can create an AutoExec macro to call this procedure automatically when the database is opened.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.