MS Access VBA does not have a built-in block comment command like some other languages, but you can comment out a block of code quickly using the toolbar buttons or by converting the lines to comments manually. The fastest method is to select the lines and click the Comment Block button on the Edit toolbar; to restore them, click Uncomment Block. If you prefer keyboard-only work, you can prefix each line with an apostrophe or use the Rem statement, though neither is a true block comment. This guide covers the exact steps, the quirks of the VBA editor, and what to do when the buttons are missing or the code still runs.
What You'll Need
- Microsoft Access installed on a Windows PC (the VBA editor is not available on Access for the web or Mac).
- A database with at least one module, form, or report containing VBA code.
- Permission to edit VBA code in the database (macro security settings may block this).
- The VBA editor open (press Alt+F11 from Access).
Step-by-Step
- Open the VBA editor. In Access, press
Alt+F11or go to the Database Tools tab and click Visual Basic. The editor opens with your project in the Project Explorer on the left. - Open the module or code-behind you want to edit. Double-click a module under Modules, or expand Microsoft Access Class Objects and double-click a form or report to open its code window.
- Select the block of code. Click at the start of the first line, hold Shift, and click at the end of the last line. You can also drag with the mouse. Include only complete statements; do not select a partial line unless you intend to comment just that part.
- Click the Comment Block button. On the Edit toolbar, look for the button with a blue checkmark and a green line (or a speech-bubble icon depending on your version). Hovering shows Comment Block. Clicking it adds an apostrophe to the start of every selected line. Labels can differ slightly by version, so if you do not see it, right-click the toolbar area and enable the Edit toolbar.
- Verify the result. Each selected line should now begin with a single apostrophe (
'). The VBA editor colours comment text green by default. If a line already had an apostrophe, the button adds another, which is harmless but messy. - To restore the code, select the commented lines and click Uncomment Block. This button sits next to Comment Block and removes one leading apostrophe per line. If you commented a line twice, you may need to click it twice.
- Save and test. Press
Ctrl+Sto save the module, then run the procedure or open the form to confirm the commented code no longer executes and the rest still works.
If the toolbar buttons are not visible, you can comment manually: select the lines and type an apostrophe at the start of the first line, then repeat for each line. There is no way to apply one apostrophe to a whole block in a single keystroke without the toolbar button. The Rem statement works the same way as an apostrophe but must be followed by a space and cannot be placed after code on the same line.
Common Mistakes
- Selecting a partial line. The Comment Block button comments the whole line, not just the selected characters, so a trailing comment mark can end up in the middle of a statement and cause a compile error.
- Forgetting that line continuations break. If a statement uses an underscore (
_) to continue onto the next line, commenting only the first line leaves the continuation lines active and causes a syntax error. Comment every line of the continued statement. - Assuming the code is disabled. Commented code still appears in the editor and can confuse later readers. Add a short note above the block explaining why it is commented out.
- Using
Remafter code on the same line.Remmust be the first statement on a line; use an apostrophe for trailing comments. - Commenting inside a string. An apostrophe inside a quoted string is just text, not a comment. Do not try to comment out part of a string literal.
Tips
- Use
Ctrl+E? No — that is not a VBA shortcut. Stick to the toolbar buttons or manual apostrophes. - Keep a backup copy of the module before commenting large blocks; it is easy to lose track of which apostrophes were original comments.
- Use the Edit toolbar's Toggle Breakpoint (F9) for temporary disabling during debugging instead of commenting, then remove the breakpoint.
- For long blocks, consider moving the code to a separate procedure and simply not calling it, which is cleaner than a wall of green text.
- Comment blocks are not preserved in compiled ACCDE files, so keep the source ACCDB.
Troubleshooting
The Comment Block button is greyed out. The code window may not have focus, or the module may be protected. Click inside the code first. If the project is password-protected, you must supply the password to edit.
The code still runs after commenting. Check that you commented the correct procedure and that no other event or macro calls the same logic. Also confirm you saved the module; unsaved edits do not take effect until you save and, for forms, close and reopen.
Compile error after uncommenting. Uncomment Block removes only one apostrophe per line. If a line had two, it remains a comment. Click Uncomment Block again on that line.
Toolbar missing entirely. Right-click any toolbar, choose Customize, and enable the Edit toolbar. In some versions the command is under View > Toolbars.
Accidental mass comment. Press Ctrl+Z immediately to undo. If you have saved, use Uncomment Block on the affected lines, or restore from a backup.
Frequently Asked Questions
Does VBA have a block comment like /* */?
No. VBA only supports line comments using an apostrophe or Rem. The Comment Block button simply adds an apostrophe to each selected line.
Can I comment out a block with a keyboard shortcut?
There is no default shortcut for Comment Block in the VBA editor. You can assign one via toolbar customization, but the built-in buttons are the standard method.
Why does my commented code turn green but still cause errors?
Green text is a comment and is ignored by the compiler. If you still get an error, a line in the block was not actually commented, often because of a line-continuation underscore or a partial selection.
Does commenting out code affect performance?
No. Comments are ignored at compile time and have no runtime cost.
Can I comment out a whole procedure?
Yes, select from Sub to End Sub and use Comment Block. The procedure will no longer be callable, so any caller will raise a compile error until you uncomment it.