Others

How to Use the Microsoft Access /cmd Option to Open a Form or Report

Nimra MalikNimra Malik Nov 25, 2026 868 views

Question details

Understand how to pass a command-line value to a Microsoft Access database and use it to choose an action at startup.

How to Use the Microsoft Access /cmd Option to Open a Form or Report
Product
Microsoft Access
Device & OS
not provided
Scenario
A user wants to launch an Access database with a command-line argument that selects a form or report.
Observed behavior
The /cmd switch supplies text to the Command() function; it does not automatically execute a macro or function with that name.
Before you start

Identify the database file and the exact names of the forms or reports you want to open. Ensure the database is trusted or its content is enabled so its startup macro and VBA code can run.

Solution 1Recommended

Use an AutoExec macro to process the /cmd value

Use this method to read a startup argument and explicitly map supported values to database actions.

Access makes the text after /cmd available through Command(). Your database must call that function and decide what to do with the returned string.

1
Create an initialization function

In Access, open the VBA editor with Alt+F11, choose Insert > Module, and add a public function in the standard module. For example: Public Function InitApplication() As Boolean Select Case Trim$(Command()) Case "form1" DoCmd.OpenForm "form1" Case "report2" DoCmd.OpenReport "report2", acViewPreview Case "" ' No startup argument supplied. Case Else MsgBox "Unsupported command-line argument: " & Command() End Select InitApplication = True End Function

2
Create the startup macro

Return to Access, choose Create > Macro, and add the RunCode action. Enter InitApplication() in its Function Name field, then save the macro as AutoExec.

3
Launch with an argument

Close the database, then use a command prompt or shortcut to launch it with a command such as: "C:\Path\To\MSACCESS.EXE" "C:\Path\To\Database.accdb" /cmd form1. Replace the paths with your actual Access executable and database paths.

4
Check the result

Open the database with /cmd form1 to test the form, or use /cmd report2 to test the report. Update the names in the VBA function to match objects in your database.

Use trusted commands: The example accepts only explicitly listed values. Do not pass an untrusted argument directly to Application.Run; use it only when the supplied value is a trusted function name.
Free Microsoft Office alternative

Work with Office documents in WPS Office

WPS Office is a free, lightweight alternative for everyday documents, spreadsheets, and presentations. Its familiar interface and support for common Microsoft Office formats make it easy to move those files between applications. It does not run Access databases or fix the Access /cmd behavior.

A familiar interface for everyday Office tasksCompatibility with common Microsoft Office formats, including DOCX, XLSX, and PPTXA lightweight option for working with spreadsheets alongside database workflowsFree tools for documents, spreadsheets, and presentations
QA img-9

Frequently Asked Questions

Does /cmd automatically run a function with the supplied name?

No. The /cmd switch passes text to Access, where Command() returns it. A startup macro or other code must read the value and choose an action.

How do I make Access process the argument when the database opens?

Create a macro named AutoExec with a RunCode action that calls a public initialization function in a standard VBA module. Have that function read Command() and handle supported values.

What does Command() return if I do not supply /cmd?

Command() returns an empty string. Include a Case "" branch or another empty-value check in your initialization function if no argument is a valid startup condition.

Can I use Application.Run with the /cmd value?

Yes, code can call Application.Run Command(), but only use this approach when the supplied value is a trusted function name. An explicit list of supported commands gives you more control over which actions can run.

Why does my AutoExec macro not run?

Check that the macro is saved with the exact name AutoExec and that the database is trusted or its content is enabled. Also check whether startup options were bypassed when the database opened.