Runtime Error 402: “Must close or hide topmost modal form first,” explained and fixed
Error 402, “Must close or hide topmost modal form first”, fires when an operation tries to bypass the topmost modal form: showing a new form out of turn, or unloading or hiding a form that sits underneath a modal one. Modal forms form a stack, and only the top of the stack is accessible. Anything that reaches past it, for a form below, or for a fresh modeless window, is refused with error 402 until the topmost modal form is closed or hidden. The message is the instruction: deal with the topmost modal form first.
For people who just run the program
- Expect it with stacked dialogs. A dialog opened from a dialog opened from a dialog: actions that try to jump back to the first one, skipping the ones on top, produce this error.
- Close the dialogs in reverse order. Finish or cancel the topmost, then the next. If the action then works, the stack explanation is confirmed.
- Note the chain of windows involved. The sequence of dialogs you opened is the reproduction recipe the maintainer needs.
- Do not force-quit unless truly wedged. Modal stacks unwind normally through their own close buttons; the error changes nothing by itself.
For people who write the code
- Respect the stack in code as the UI does. Before showing or unloading, ask what is on top. If your code needs a lower form, close or hide the modal forms above it first, in reverse order of showing.
- Hide is a legitimate release valve. The message itself allows it: hiding the topmost modal form relinquishes its grip without destroying its state, letting the underlying operation proceed and the dialog resume later.
' frmWizard (modal) is on top of frmMain.
' frmMain.Show / Unload frmMain here raises error 402.
frmWizard.Hide ' release the top of the stack
Unload frmMain ' now legal
' Or close frmWizard entirely if its work is done.
- Audit cross-form calls. Event code in a lower form that fires while a modal form is up (timers are the usual suspects) attempts out-of-turn operations constantly. Guard such handlers with a check that no modal form is active.
Timer events do not respect modality, but error 402 enforces it. A timer on a buried form keeps firing under a modal stack and its handler's first form operation raises 402. Disable timers when showing modal forms above their owners, or check state inside the handler.
If it still happens
If the stack looks empty and 402 persists, a modal form may be open but not visible in the Forms enumeration you checked (a hidden modal form still counts as topmost). Look for forms shown with vbModal anywhere in the session's history, including error paths that showed one and then failed before closing it.
Common questions
What is the “topmost” modal form? The most recently shown modal form that is still open. Modal forms stack: each new one sits on top of the previous, and only the top one can be interacted with or must be dealt with first.
How do errors 400, 401, and 402 differ? All three police modality. 400: a displayed form cannot be re-shown modally. 401: no modeless form may appear while a modal one is up. 402: no operation may reach past the topmost modal form. Same discipline, three directions of violation.
Related errors