Microsoft Access Command-Line Switches

Windows Command Prompt Icon

I thought I’d covered a basic aspect of Microsoft Access, the Command-Line Switches, and how to use them.

Access Command-Line Switches

So what Command-Line Switches are available to us exactly? Below are lists of Official and Not So Official Microsoft Access Command-Line Switches.

Official Switches

Command-Line Switch Microsoft’s Definition My Plain English Definition
Opens the specified database or Microsoft Access project. You can include a path, if necessary. If the path contains spaces, enclose it in quotation marks. Not including a switch opens the database normally
/cmd Specifies that what follows on the command line is the value that will be returned by the Command function. This option must be the last switch on the command line. You can use a semicolon (;) as an alternative to /cmd.

Use this switch to specify a command-line argument that can be used in Visual Basic for Applications (VBA) code.

Is used to pass a value into the db and the value can then be retrieve using the Command() function (yes, there is a VBA function named Command – A command named Command!) via VBA
/compact Compacts and repairs the Access database, or compacts the Access project that was specified before the /compact switch, and then closes Access. If you omit a target file name following the /compact switch, the file is compacted to the original name and folder. To compact to a different name, specify a target file. If you include a path that contains spaces, enclose the path in quotation marks.

If you specify a file name in the target database or target Access project parameter but you don’t include a path, the target file is created in the default database folder that is specified in Access. You can change this setting in the Access Options dialog box.

In an Access project, this option compacts the Access project (.adp) file but not the Microsoft SQL Server database.

Compact the database

Pops the db open, compacts, then closes it

/excl Opens the specified Access database for exclusive access. To open the database for shared use in a multiuser environment, omit this switch. Applies to Access databases only. Open the database in Exclusive mode
/profile Starts Access by using the options in the specified user profile instead of the standard Windows Registry settings created when you installed Access. This replaces the /ini switch used in versions of Access prior to Access 95 to specify an initialization file. An override to open Access using the specified profile rather than the one used to install Access.
/ro Opens the specified Access database or Access project for read-only use. Open the database in Read-Only mode
/runtime Specifies that Access will start with runtime version options. Open the database simulating runtime environment
/x Opens the Access database that was specified before the /x switch, and then runs the specified macro. Another way to run a macro when you open a database is to create a macro named AutoExec.

Macros can contain viruses, so you must be careful about running them. Take the following precautions: run up-to-date antivirus software on your computer; use the Trust Center to disable all macros except those that are digitally signed; maintain a list of trusted sources of macros.

Open the database and run the specified Macro

Older or Undocumented/Unofficial Switches

Many of the are no longer valid as of Access 2007 accdbs, but remain for backwards compatibility with mdb databases.

Command-Line Switch Microsoft’s Definition My Plain English Definition
/decompile undocumented Open the database and decompile the VBA Project, VBA code cleanup.
/nostartup Open the database without displaying the startup dialog (also referred to as the Task Pane)
/pwd Starts Access using the specified password. This was used with ULS to specify the user’s password to use when launching the database.
This is not for the database password!
/repair Repairs the Access database specified before the /repair option and then closes Access. In Access 2000, compact and repair functionality is combined under /compact. The /repair option is supported for backward compatibility. Repairs the specified database.  Now superseded by the /compact switch
/wrkgrp Starts Access using the specified workgroup information file. Open the database using the specified workgroup file
/user Starts Access using the specified user name. This was used with ULS to specify the username to use when launching the database.

How-To Use Access Command-Line Switches

Did Someone Say Dos Prompt/Command Prompt?

That’s right, to use command-line switches we need a Command Prompt window!  Yes, the good old 1980’s Dos window.

There are numerous way to launch the Command Prompt windows, but here is one approach:

  • Press the Windows Key + R (this will launch the run window)
  • Type cmd in the Open textbox
  • Click on the Ok button (this should launch the Command Prompt window

Now that we have a Command Prompt we are ready to launch Access with our Command-Line Switches.

Yes, Batch (*.bat) files and the likes are equally good for automating/running command-line switches.

Running the Command-Line Switches

The basic syntax to execute command-line switches comes in 2 forms:

Including the Application Path

"PathAndExeFilename" "PathAndDatabaseFilename" /switch

Omitting the Application Path

"PathAndDatabaseFilename" /switch

On most systems the path to Microsoft Office executable will already be part of the environment path variable.  As such, we should be able to use the simplified approach and thus omit the application path when using command-line switches.

Concrete Examples

Here are a few concrete example of command-line switch syntax/usage.

Opening a Database Normally
"C:\Users\Daniel\Documents\Demo.accdb"

Or, if we wanted to use the Application path approach, it would look like:

"C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\Users\Daniel\Documents\Demo.accdb"
Using /cmd

This one a little trickier.

"C:\Users\Daniel\Documents\Demo.accdb" /cmd "Some Value"

and then within the database your could simply use an AutoExec Macro and do

MsgBox "'" & Command & "'"

When the database is launched you should then get a message box stating

‘Some Value      ‘

Notice the extra spaces returned by the function!  So this would be a great time to probably utilize the Trim() function.

One useful thing you can do with /cmd is use it to pass a specific Function you want run.

"C:\Users\Daniel\Documents\Demo.accdb" /cmd "YourFunctionName"

and then, using your AutoExec macro you can run it by simply doing

Application.Run Trim(Command) 'Don't forget the Trim()!!!
/compact
"C:\Users\Daniel\Documents\Demo.accdb" /compact
/decompile
"C:\Users\Daniel\Documents\Demo.accdb" /decompile
/excl
"C:\Users\Daniel\Documents\Demo.accdb" /excl
/ro
"C:\Users\Daniel\Documents\Demo.accdb" /ro
/runtime
"C:\Users\Daniel\Documents\Demo.accdb" /runtime
/x
"C:\Users\Daniel\Documents\Demo.accdb" /x "YourMacroName"

Note, upon my testing, the specified macro runs after any AutoExec macros already setup in the database.

Chaining Command-Line Switches Together

One of the beautiful things about Command-Line Switches is that they aren’t exclusive, so we can actually chain multiple switches together in a single command.

Say we wanted to open a database in runtime mode and run a specific macro we could simply do:

"C:\Users\Daniel\Documents\Demo.accdb" /runtime /x "YourMacroName"

Thus, drastically increasing the usefulness of Command-Line Switches.

Command Case Sensitivity

It should be noted that the Commands are case insensitive.  So

"C:\Users\Daniel\Documents\Demo.accdb" /runtime

is just as valid as:

"c:\USERs\Daniel\DocuMENts\demo.accdb" /RUNTIME

 

The Access Context Menu

You’re probably saying to yourself:

There has to be an easier way than using the Command Prompt…

Well, you’re in luck because there is!

Several years ago, I created a FREE utility (no strings attached) that gives Windows Explorer access to Command-Line switches directly.  Thus, enabling you to simply right-click on any database to execute the command-line switch of your choosing.  To learn more please check out

Or watch the YouTube at the top of this article for more information.

Useful Links on the Subject

2 responses on “Microsoft Access Command-Line Switches

  1. Peter Kaye

    Is there any way to open a run-time database which has been encrypted and a database password in VBA code. The application is to deliver a code protected application which starts by opening a login form for each user to provide their credentials. The database password is only know to the developer whereas user credentails can be maintained within the application. Thanks.

    1. Daniel Pineault Post author

      You would need to use Access automation and the OpenCurrentDatabase method to supply the password. Maybe something like:

      Function OpenPwdProtectedAccde()
      On Error GoTo Error_Handler
          Dim oAccess                   As Access.Application
          Const sDb = "C:\Users\Dev\Desktop\Client-CRM.accde" ' Path to the accde
          
          ' Open the accde file
          Set oAccess = New Access.Application
          oAccess.OpenCurrentDatabase sDb, False, "YourDbPassword"
          
          ' Close the 'host' db
          Application.Quit
          
      Error_Handler_Exit:
          On Error Resume Next
          Set oAccess = Nothing
          Exit Function
       
      Error_Handler:
          MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
                 "Error Source: OpenPwdProtectedAccde" & vbCrLf & _
                 "Error Number: " & Err.Number & vbCrLf & _
                 "Error Description: " & Err.Description & _
                 Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                 , vbOKOnly + vbCritical, "An Error has Occurred!"
          Resume Error_Handler_Exit
      End Function

      You could also ensure it has the focus by doing:

      #If VBA7 Then
          Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As LongPtr) As Long
      #Else
          Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
      #End If
      
      
      Function OpenPwdProtectedAccde()
      On Error GoTo Error_Handler
          Dim oAccess                   As Access.Application
          Const sDb = "C:\Users\Dev\Desktop\Client-CRM.accde" ' Path to the accde
          
          ' Open the accde file
          Set oAccess = New Access.Application
          oAccess.OpenCurrentDatabase sDb, False, "YourDbPassword"
          SetForegroundWindow oAccess.hWndAccessApp
          
          ' Close the 'host' db
          Application.Quit
          
      Error_Handler_Exit:
          On Error Resume Next
          Set oAccess = Nothing
          Exit Function
       
      Error_Handler:
          MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
                 "Error Source: OpenPwdProtectedAccde" & vbCrLf & _
                 "Error Number: " & Err.Number & vbCrLf & _
                 "Error Description: " & Err.Description & _
                 Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                 , vbOKOnly + vbCritical, "An Error has Occurred!"
          Resume Error_Handler_Exit
      End Function

      This however will not work in a runtime environment, but you didn’t mention that so I don’t think it is an issue here. In such a case, you’d have to launch Access by using the path to the msaccess.exe, then bind to that instance, then open the accde, …