Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modal forms causing VBA to fail

Status
Not open for further replies.

JULL

Technical User
Mar 12, 2009
6
US
Problem: I have two forms running modal. You can jump from form 1 to 2 and 2 to 1. When users close 2, it jumps back to 1 automatically. Form 2 will stop to respond if you jump back and forth by closing form 2 couple of times.

I guess because of the modality of showing form 1, the termination code was never completely executed. It is fine to suspend the execution of codes with other events, but somehow Microsoft thought you cannot terminate twice.

In MS Access you can open a modal form and continue to execute the codes. I like that approach better. Why suspend execution of codes because of modality? That takes away some power from the programmers and adds extra burden to memory usage. If the users jump from one modal form to another many times, there are a lot of continuing codes to be executed and most of the time such codes are "end sub" type statements.

I think there should be a command that load modal forms and continue to execute the subsequent codes, or build the show method with such an option.
 
In MS Access you can open a modal form and continue to execute the codes
Really ?
 
Yes. You can even open multiple modal forms with codes. You can also change modality of forms at run time.
 
What application you are in and how do you switch between modal forms? Please show sample code.

combo
 
I'm in Powerpoint. For sample code, create two forms, let's call them form1 and form2. With form1, create a button or simply put the following code into the terminate event to call show form2:

form2.show

Of course, if you use a button, you have to hide form1 or unload it first.

With form2, put the following code into the terminate event:

form1.show

Then use a procedure in a module to open the forms. Try to jump from one to the other. You'll see. The forms will stop to respond when you try to run the form show procedure the second time in the terminate event.

I also tested it in Excel. It behaves the same.
 
You never finish the 'Terminate' event for the first form. The sequence of events:
1. create and show form1,
2. start terminate form1 (hide it) and show form2,
3. start terminate form2 (hide it) and show form1 again,
4. code stops as you can't terminate form1 (step 2).

In practice you are not jumping between forms, you try to create a new instance every time.

To jump, try (assuming userform1 is shown first):
Code:
Public frm2 As UserForm2

Private Sub cmdShowForm2_Click()
Me.Hide
If frm2 Is Nothing Then Set frm2 = UserForm2
Set frm2.frm1 = Me
frm2.Show
End Sub

Private Sub UserForm_Terminate()
Unload frm2
Set frm2 = Nothing
End Sub
Code:
Public frm1 As UserForm1

Private Sub cmdShowForm1_Click()
Me.Hide
frm1.Show
End Sub

Private Sub UserForm_Terminate()
Unload frm1
Set frm1 = Nothing
End Sub


combo
 
I'm still a bit confused by:
In MS Access you can open a modal form and continue to execute the codes
Could you show us how that's happening (also opening multiple modal forms in Access)?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
In MS Access, you use docmd.openform method. Modality of a form is predefined, but you can change it at run time. Opening a modal form will not halt the execution of codes. You can open multiple modal forms. Access forms are quite different in that you have much more control over their behaviors.

Opening modal forms without halting the execution of subsequent codes makes intuitive sense. Modality is to prevent users from getting away from the modal forms, not to prevent programmers to do something after the forms are open. I just do not like the fact that with modal forms, there are lots of unfinished code streams waiting to be executed if the users interact with modal forms extensively.
 
Combo,

To jump from one form to another is not the issue. You can simply create command button with following codes for both forms:

me.hide
form(1/2).show

There is no problem with this approach. But my concern is to reload and show form 1 whenever form 2 is terminated. The users may not click the jump button and instead they close form 2. Then I want to force show form 1. Because execution of codes is halted, the termination events are never completed. That's the issue.

Thanks for your reply.
 
You can use QueryClose event to check if and how it is trying to be closed. You can cancel the event.
BTW, you can use the Activate event to reset the form. Depending on the contents of both forms, you can
use one form with multipage with hidden tabs and navigate between pages instead of using two forms. You can use a procedure in standard module to manage forms too.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top