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,TypeandEnumonly 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 ofFor) but declared nowhere in the project, and the module hasOption 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
- Open the database (
build/<configuration>/<name>.accdbof 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. - A missing reference: add it to
source/references.json(or set it in Access and export). - A declaration after the first procedure: move the line named in the message up, above the first
Sub,FunctionorPropertyof the module. - A variable that is not defined: declare it with
Dim(or at module level), or correct the mistyped name. - A label or name that is a keyword: rename it, for example to
DoneorFailed. - A name declared twice (a constant, a variable or a procedure in two modules or twice in one): rename or remove one.