Runtime Error 401: “Can't show non-modal form when modal form is displayed,” explained and fixed

Error 401, “Can't show non-modal form when modal form is displayed”, is the mirror image of error 400. While a modal form is up, it owns the application: the user, and the code, must finish with it before anything else happens. Code that tries to show a modeless form during that time, often a progress window, a notification, or a helper panel triggered by the modal form's own code, is refused with error 401. The restriction is the definition of modal; the bug is attempting an end-run around it.

For people who just run the program

  1. Expect it when a dialog tries to open something else. A settings dialog that wants to pop up a helper window, or a wizard step that opens a preview, while the dialog itself is modal, is the classic scene.
  2. Finish or cancel the open dialog first. Once the modal dialog closes, the same action usually succeeds, which confirms the cause and is worth reporting exactly that way.
  3. Note what the modal dialog was. “It happens while the export dialog is open” is the actionable half of the report.
  4. Nothing about the documents matters here. This is purely about which windows were open in which order.

For people who write the code

  1. Do not fight modality; design within it. If a modal dialog needs auxiliary display, put the auxiliary content inside the modal form (a frame, a second page of a tab control) rather than in a separate modeless window.
  2. If the helper must be a form, make it modal too, in sequence. A modal form may show another modal form; the stack unwinds in reverse order. Trying to make the second one modeless is what raises 401.
  3. ' frmExport is showing modally. From its code:
    ' frmProgress.Show vbModeless   ' error 401
    ' frmProgress.Show vbModal      ' legal: modal over modal
  4. For progress during modal work, update a control, not a window. A label or progress bar on the modal form itself, refreshed with the form's own repaint, gives feedback without violating modal ownership.
DoEvents does not lift modal restrictions. Pumping messages keeps the UI alive but does not change which forms may be shown. Code that adds DoEvents hoping to sneak a modeless window past a modal dialog still gets error 401, just later.

If it still happens

If no modal form is knowingly open, one may be open invisibly: a modal form shown and then hidden remains modal and still owns the application. Enumerate the Forms collection and their Visible and modal state; a hidden modal form is a known cause of “401 from nowhere.”

Common questions

Why can a modal form show another modal form but not a modeless one? Because modality is a stack discipline: each new modal layer waits for the one above it. A modeless window would float free of that stack and could be used while the modal dialog waits, which is exactly what modal exists to prevent.

Is error 401 the same as error 400? They are mirrors. Error 400: showing modally a form already displayed modelessly. Error 401: showing modelessly anything while a modal form is displayed. Both are fixed by sequencing, not by changing the forms themselves.

Related errors

Runtime Error 400 · Runtime Error 402 · Runtime Error 365