Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modeless form that still pauses code execution

Is there anyway to have a userform that acts modeless, while still pausing code execution like a modal form?

I'd like the userform to show, but still allow interaction with the parent program. Modal forms block interaction with the parent program. A modeless form would work, but I would like the code execution to pause while the form is up.

I've worked around this by creating an infinite loop that checks if the form is visible, but that seems a bit hacky.

Public Sub GetFormInfoAndDoStuff    
  ufForm.show vbModeless

  Do while ufForm.Visible
    DoEvents
  Loop

  ' Do other stuff dependent on form 
End Sub

EDITED to clarify that code after .show exists which must execute after the user form is done

like image 717
lfrandom Avatar asked May 31 '13 13:05

lfrandom


People also ask

What is the difference between a modal form and a modeless form?

Modal dialog boxes, which require the user to respond before continuing the program. Modeless dialog boxes, which stay on the screen and are available for use at any time but permit other user activities.

What is modeless form?

A modeless form is a window that is displayed until it is either obscured by another window or until it is closed or minimized by the user. Using RAD Studio, the following procedure creates a modeless form dynamically. Building this VCL application consists of the following steps: Create the project directory.

How do I make UserForm modeless?

How to Make a Modless Form with Code. vbModeless is what tells Excel to make the form Modeless. You put this after the line that you use to display the form. You do not have to put any additional code within the code section for the UserForm itself; merely add vbModeless after .


1 Answers

You should be able display the form as vbModeless and only execute code when specifically requested, i.e., from a CommandButton or other control.

You then leave the form visible/shown until it is specifically closed, via the "X" button or via another control which calls the UserForm_Terminate event.

In order to achieve this, you may need to move some of your executable code in to another subroutine and/or module, and call this subroutine for example from a CommandButton_Click event.

You already have a subroutine somewhere that contains a line like:

Sub ShowTheForm()

    UserForm1.Show vbModeless
End Sub

So the form is displayed properly to allow user-input to the parent application.

You don't really need to put any other code in the above module. We will put the other code in other modules/subs, and then call it from user controls like command buttons.

Example:

Take all of your executable code, and put it in another subroutine (and if it suits your organizational preference, another module), like:

Sub MyMacro(msg$)
    MsgBox msg
End Sub

On the UserForm, add a command button and assign it the following code:

Sub CommandButton1_Click()
    MyMacro "hello"
End Sub

Now, the form will display until the user clicks the "X" button. Code will only run when called from the command button.

EDIT FOR CLARIFICATION

You don't need to "pause" the execution using this method. Execution ends once the form is displayed modelessly, and the form persists. The object has some events which you may use to trigger further execution of code.

like image 86
David Zemens Avatar answered Oct 03 '22 20:10

David Zemens