If Access throws a macro-security or startup error before VBA runs, fix the trust settings or startup path first. If the code starts and then fails, use Debug, Err.Number, and Err.Description to find the failing line. This guide separates those checks from VBA runtime debugging so you can get to the right fix faster.
What you’ll need
- A copy of the database file for safe testing.
- Access with access to the VBA editor.
- The Immediate Window, opened with Ctrl+G or View > Immediate Window.
- Permission to open Tools > References and Debug > Compile [DatabaseName].
- A way to reopen the database with Shift held down for startup checks.
How do you tell a blocked macro from broken code?
If Access blocks the macro before code runs, the fix is usually Trust Center or trusted-location settings, not VBA edits. If the form opens, the macro starts, and only then fails, move to code-level debugging. Keep those paths separate; they need different tools and different fixes.
- Check trust settings first. Open File > Options > Trust Center > Trust Center Settings > Macro Settings and Trusted Locations, then confirm the database is permitted to run. If content is blocked, allow the file only in a location you trust and test again.
- Bypass startup code. Hold Shift while opening the database to skip startup behavior, then open the macro, form, or module directly. If the error disappears, the failure is tied to AutoExec, a startup form, or an event that runs on open. (Microsoft Support)
- Test the startup path. Open the AutoExec macro or the startup form itself and run the same action again. That shows whether the failure comes from startup code, a form event, or a manual run of the macro.
Suppose a database opens to a blank screen when Shift is held, but errors as soon as it starts normally. That pattern points to startup code, not a broken button click. Focus on AutoExec, the startup form, and any event code that fires on open.
How do you find the exact failing line?
When the code runs far enough to fail in VBA, use Debug in the VBA editor and the Immediate Window to identify the line and the error number. The highlighted line is the current fault point, and Err.Number plus Err.Description tell you what Access thinks went wrong. Microsoft’s debug flow starts there.
- Click Debug. In the error dialog, click Debug to open the VBA editor with the failing line highlighted in yellow. That line is the one to inspect first, even if the real cause is a line above it.
- Read the immediate state. Open the Immediate Window with Ctrl+G or View > Immediate Window, then inspect Err.Number and Err.Description after the break. Those values help separate a type problem from a missing object or a bad SQL string.
- Check the previous action. Review the macro action or code line immediately before the failure. A bad argument, bad control name, or bad return value often causes the next line to fail, not the one highlighted.
A common first-timer mistake is to stare only at the yellow line and ignore the line before it. That can hide a bad assignment or a missing reference that surfaces one statement later.
What should you do if Access shows a missing reference in VBA?
A missing reference is a compile-level problem, so fix it before chasing runtime symptoms. Broken references, undeclared variables, syntax mistakes, and missing functions can stop code before the real runtime line is even reached. Clean the project, then rerun the same path.
- Open Tools > References. In the VBA editor, go to Tools > References and look for any entry marked MISSING. Clear the bad reference if it is no longer valid, or replace it with the correct library for this database.
- Compile the project. Choose Debug > Compile [DatabaseName] to force Access to check syntax and structure. This catches undeclared variables, non-existent functions, and broken references before they become harder-to-read runtime failures.
- Retest the same path. Reopen the exact form, macro, or report that failed before. If the compile step exposes another error, fix that one next and compile again.
After a migration or Office update, a reference can break without any visible change to your code. That is why a compile pass belongs near the top of the list, not the bottom.
How do I fix type mismatch error 13 in access VBA?
Type Mismatch error 13 often comes from Null handling or assigning the wrong type to a variable or field. Null values are the most common cause. Use Nz() with a default that matches the destination type, then check string, number, and date assignments separately.
- Check for Null first. Look at the source value in the Immediate Window or in a watch expression and confirm whether it can be Null. If it can, wrap the read with Nz() before it reaches the assignment.
- Match the destination type. Use Nz(rs!FieldName, 0) for number fields, Nz(rs!FieldName, "") for string fields, and handle date fields so a Null value does not get forced into a placeholder date. That keeps the fallback value compatible with the variable or control.
- Verify the target declaration. Confirm the receiving variable or control expects the same type you are writing. A text box bound to a date, or a numeric variable receiving a text fragment, will trigger a mismatch.
Say your code reads a blank date from a recordset and pushes it into a Date variable. Without a proper Null check, the assignment can fail on the first empty record and never reach later rows.
How do I fix object required error 424 in Access VBA?
Error 424 often indicates that Access expected an object but got a missing reference, a bad control name, or a plain value where an object was required. For object assignments, the Set keyword matters. Check the form, report, or control name before changing deeper code.
- Verify the object name. Check the exact form, report, table, query, or control name used in the code. A renamed control or a typo in Me!ControlName can make the object reference fail even when the code compiles.
- Use Set for objects. When assigning an object variable, use Set instead of a plain assignment. This is required for object references and helps Access know you are binding to an object instance, not copying a value.
- Confirm the object exists. Open the target form or report directly and verify it still exists in the database. If the object was renamed, deleted, or replaced, update every reference before rerunning the macro.
How do I debug a dynamic SQL string that fails in Access VBA?
When Access raises Syntax error in query expression, the problem is often the SQL string, not the VBA line that executes it. Print the final SQL, inspect the quotes, and check date delimiters. Text values need the quote style expected by the query, dates need hash marks, and apostrophes inside text must be escaped with Replace().
- Print the final SQL. Use Debug.Print strSQL before execution, then copy the printed text from the Immediate Window. That shows the exact string Access will send to the query engine.
- Check quotes and dates. Put text values in the quote style expected by the query and dates in hash marks, such as #1/1/1900#. If a value contains an apostrophe, wrap it with Replace(value, "'", "''") before building the SQL.
- Run the SQL alone. Paste the printed SQL into a query and run it by itself. If it fails there too, the problem is the expression, not the VBA procedure.
How can I stop Access macros from failing after an Office update?
After an update, the first checks are broken references, compile errors, trusted-location policy, and version drift between machines. A database can fail on one workstation if the update changed a reference, tightened macro settings, or exposed an old startup path. Reproduce on a copy before changing production code.
- Retest on a copy. Make a duplicate database file and open the same startup path there first. That limits risk while you sort out whether the update broke a reference, a startup form, or a security setting.
- Recheck trust settings. Go back to File > Options > Trust Center > Trusted Locations and Macro Settings, then confirm the database is still allowed to run. An update can reset policy or reveal a path that is no longer trusted.
- Compile and reopen. Run Debug > Compile [DatabaseName], save, close Access, and reopen the same file. If the problem returns only after reopen, the startup path or a missing reference is still in play.
How do I add error handling so my Access macro does not crash?
Use On Error GoTo in VBA procedures where you want controlled cleanup and On Error Resume Next only where a skipped failure is acceptable. In Access macros, the OnError macro action lets you choose what happens when an error occurs. If you set Go to to Fail, Access stops the macro and shows an error message.
- Add a handler first. Place On Error GoTo in the procedure entry point, then route failures to a clear exit section. That keeps the code from stopping in the middle of a partial update.
- Use Resume Next sparingly. Apply On Error Resume Next only around a line that can safely fail and be checked immediately after. Clear the error handling once that line is done so later failures are not hidden.
- Set macro error behavior. In a macro, add the OnError action and choose the Go to setting you want. If you choose Fail, Access stops the current macro and displays an error message. (Microsoft Support)
Microsoft access vba macro triage matrix
This matrix starts with the symptom you see and points to the first test to run. Use it before editing code so you do not chase the wrong layer.
| Symptom | Likely cause | First test |
|---|---|---|
| Macro will not run at all | Trusted Locations or macro settings blocked it | Open File > Options > Trust Center > Trust Center Settings |
| Error appears on open | AutoExec or startup form failure | Hold Shift while opening the database |
| Debug is unavailable | Macro blocked before VBA runs | Bypass startup and check trust settings first |
| Type Mismatch 13 | Null value or wrong data type | Check the source with Nz() and match the target type |
| Object Required 424 | Missing object, form, or control reference | Verify the object name and confirm Set is used |
| Syntax error in query expression 3075 | Bad SQL string or invalid delimiters | Debug.Print strSQL before execution |
| Compile error before runtime | Missing reference or syntax issue | Run Tools > References, then Debug > Compile [DatabaseName] |
| Code works on one PC only | Version drift or broken reference on one machine | Compare references and compile on both machines |
| Button click fails after rename | Control name changed but code did not | Check the exact control name on the form or report |
| Query fails on apostrophe | Unescaped text value in SQL | Use Replace(value, "'", "''") |
| Date filter fails | Date literal not wrapped correctly | Use hash marks around the date value |
| Startup macro fails after update | Trust Center or reference changed after Office update | Retest a copy, then recheck trust and compile |
Common mistakes
- Editing VBA first when the database is blocked at the trust layer.
- Skipping Debug > Compile [DatabaseName] after fixing a reference.
- Testing only the button click and not the startup form or AutoExec path.
- Ignoring the line before the highlighted line in the VBA editor.
- Leaving a text value unquoted or a date without hash marks in SQL.
Prevention: keep a trusted test copy of the database and rerun compile after every reference change or Office update.
Frequently asked questions
Why does my Microsoft Access VBA macro cause a runtime error?
Start by splitting the failure into two cases: the macro is blocked before code runs, or VBA starts and then fails. Use Trust Center and trusted locations for the first case, and Debug, Err.Number, and Err.Description for the second. That split keeps you in the right editor from the start.
How do I find the exact line causing the Access VBA runtime error?
Click Debug in the error dialog, then use the VBA editor’s yellow highlight to inspect the current line. Open the Immediate Window with Ctrl+G or View > Immediate Window and check Err.Number and Err.Description. If the line looks fine, inspect the action or statement immediately before it.