HOW TO: Compile to .accde or .mde From the Command Line

"The Joel Test" requires that you can "make a build in one step." With these instructions, you can create an .accde from an .accdb without even opening Access.

HOW TO: Compile to .accde or .mde From the Command Line

In yesterday's article, I shared a method to create an .accde or .mde from the current Access file:

CompileCurrentAccessApp(): Use VBA to Create an ACCDE or MDE of the Currently Open Access File
This function allows you to create an .accde or .mde file based on your current Access file with a single press of the F5 key.

But what if you wanted to create an .accde or .mde from the command line?  Perhaps as part of a build pipeline.  You know, so that you can "make a build in one step."  

Here's one way to do that.

Step-by-Step Instructions

Step 1. Add the CompileCurrentAccessApp() Procedure to Your Database

Start by copying and pasting the code for the CompileCurrentAccessApp() procedure into the Access file you want to be able to compile.

For me, that method lives in my DesignProcedures standard module, which is one of several modules from my code library that I include in all of my Access applications.

Step 2. Create a Startup Routine that Checks VBA.Command

Copy and paste the code below into a standard code module:

Public Function Startup()
    Select Case Trim(VBA.Command$)
    Case "CompileMe"
        CompileCurrentAccessApp
        Application.Quit
    End Select
End Function

The above code makes use of the VBA.Command function:

When [Microsoft Access] is launched from the command line, any portion of the command line that follows /cmd is passed to the program as the command-line argument.

Note that the argument typically includes leading and/or trailing whitespace, so the best practice is to use Trim() before doing any string comparisons.

Step 3. Call Startup() From an AutoExec Macro

There are two main ways to run code whenever an Access application starts up:

  • From the Load() or Open() event of a startup form (set via Options > Current Database > Display Form)
  • From an AutoExec macro

We'll cover the AutoExec macro approach here.  

  1. Create an AutoExec macro following the instructions in the link above
  2. Add a RunCode action and set the "Function Name" to Startup()

Note that macros cannot call Sub's directly, so Startup() must be a Function.

Step 4. Launch the Application From the Command Line

Run the following command from the cmd window:

"C:\Full\Path\To\MSACCESS.EXE" "C:\Path\To\MyApp.accdb" /cmd CompileMe

If you want, you can save the above command to a batch file (i.e., a text file with a .bat extension).  Then you can simply double-click on the batch file to compile your front-end on demand.

And now you can "make a build in one step."  Mr. Spolsky would be proud.

Cover image created with DALL-E-3.

All original code samples by Mike Wolfe are licensed under CC BY 4.0