How to Use the Microsoft Access /cmd Option to Open a Form or Report
Question details
Understand how to pass a command-line value to a Microsoft Access database and use it to choose an action at startup.

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

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.




