Color scheme: Riviera
You are here: start » en » Documentation » Microsoft Access development tools » WKL Office SDK » Troubleshooting » VBA code does not compile

VBA code does not compile

Symptom: build ends with exit code 4 and names one or more objects whose code does not compile.

Environment

All versions of the WKL Office SDK.

Cause

Access compiles the whole VBA project and names no module. The build therefore searches: it removes the bodies of all procedures, then gives each object its full code back, and reports the objects that break the compile. When even the reduced project does not compile, the error is in a declarations section or a procedure header - typically a type from a reference missing in references.json - and the objects whose declarations break it are reported.

Some causes are found in the sources before Access starts, with file and line; check and build report them as error [Compile] module <name>, <file>:<line>:

  • A declaration after the first procedure (WKL Office SDK 0.6.1 and later). VBA allows Dim, Const, module variables (Private/Public), Declare, Type and Enum only in the declarations section at the top of a module; one further down breaks the compile of the whole project.
  • A variable that is not defined: a name is assigned a value (total = 1, Set rs = …, the counter of For) but declared nowhere in the project, and the module has Option Explicit. Only assigned names in standard and class modules are checked.
  • A keyword or statement of VBA used as a label (Write:, Name:), or a reserved word used as the name of a parameter, variable or procedure (ByVal Until As Date).
  • A module named like the VBA project (the name of the database); Access does not load it.

The same check runs in the Access add-in with Check code and after every save in the VBA editor, there on the code as it stands in the editor (Use the Access add-in).

Resolution

  1. Open the database (build/<configuration>/<name>.accdb of the last good build, or the one Access has open) and use Debug > Compile in the VBA editor: it shows the line. The SDK cannot report the line, because only the VBA editor knows it and shows it in a message box.
  2. A missing reference: add it to source/references.json (or set it in Access and export).
  3. A declaration after the first procedure: move the line named in the message up, above the first Sub, Function or Property of the module.
  4. A variable that is not defined: declare it with Dim (or at module level), or correct the mistyped name.
  5. A label or name that is a keyword: rename it, for example to Done or Failed.
  6. A name declared twice (a constant, a variable or a procedure in two modules or twice in one): rename or remove one.
en/wiki/msaccess/sdk/troubleshooting/vba-code-does-not-compile.txt · Last modified: by Wolfgang van der Stille