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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execution beyond modal form

Status
Not open for further replies.

IDoWindows

Programmer
May 18, 2001
14
US
I’ve got a strange situation here, and I wanted to see if it rings any bells with anyone. When I run my main form code from within VB, with no breakpoints, I get the error “Can’t show non-modal form when a modal form is displayed.” Debugging at this point reveals the offending "form show" some 40 lines later. Like this:

If mvAccountType = "Admin" Then
frmEnterSchool.Show vbModal, Me
End If
.

.

.
frmStudents.Show

Now, if I put a msgbox just before the "If", forcing the code to stop execution until I click “OK”, then it continues and executes as expected, with the frmEnterSchool form vbmodal and holding up further processing until frmEnterSchool unloads.

Equally as curious, when I remove the msgbox and put in a simple breakpoint to pause processing at the "If", to verify that I am logged in with “Admin” account type, then press F5 to continue processing, the modal frmEnterSchool behaves properly, also. Same results using the “run to cursor” debugging tool, with cursor on the "If"… It’s a puzzler.
 
If I am understanding your question correctly you will need to break your procedure or open the frmEnterSchool form as non modal. You will need to stop the execution of the code after you open the frmEnterSchool form or wait for the frmEnterSchool is closed until you show the frmStudents form. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
I think you have a timing issue here. Its my understanding that when you issue a load command, you are issuing messages to windows for it to process, and those message as processed asynchonously, so execution continues within your code, issuing additional messages - the second form load - until the modal message is processed by windows. I think what may be happening is that you get to the second form load before the modal status is assumed, so that when windows encounters the second form load message after the modal status has been set, it generates the error.

That's where the DoEvents comes into play - you're asking Windows to process its outstanding messages before you continue with processing.


I would place a DoEvents after the first form load

If mvAccountType = "Admin" Then
frmEnterSchool.Show vbModal, Me
DoEvents
End If


If my understanding of what's happening is not correct (wouldn't be the first time, probably not the last either), I looking forward to being set straight (strongm - are you here? :))

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
That's exactly the advice I just received from our in-house guru -- but to no avail! With the DoEvents immediately after the modal form load, I still get the error (which actually comes from a form load within the frmStudents sub Form_Load).

With the DoEvents before the If, I get no error, but the frmStudents form loads first...
 
I missed the question sorry. IDoWindows, is there the posibility that the procedure that contains the
If mvAccountType = "Admin" Then
frmEnterSchool.Show vbModal, Me
End If
.

.

.
frmStudents.Show

is being called a second time or from another procedure? I am kind of grasping at straws here but I could not duplicate the problem. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Perhaps a way to work around the problem would be to do the following:

In your main form, create a public boolean variable, with form-level scope

Public fBol_LoadingForm as Boolean

Then inside your routine, modify the load section as follows:

If mvAccountType = "Admin" Then
fBol_LoadingForm = True
frmEnterSchool.Show vbModal, Me
Do While (fBol_LoadingForm = True)
DoEvents
Loop
End If

Now, inside frmEnterSchool, I would create a timer (tmrStartUp) with an interval of 500, roughly half a second, initially enabled.

Create a timer event in frmEnterSchool to read as follows:

Private Sub tmrStartUp_Timer()

tmrStartUp.Enabled = False
frmMain.fBol_LoadingForm = False

End Sub

If this works, then once the main form loads the EnterSchool form, it will go into a DoEvents loop. The frmEnterSchool will, from the StartUp event set the loop to control to false, allowing the main form to exit the loop. But at this point, the EnterSchool form should be active and modal so that execution will cease in the mainform until the EnterSchool form is unloaded. Upon the unload, execution should resume after the loop.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Stranger and stranger... the DoEvents statement didn't work, and I tried it in different locations. I then stepped through my program step-by-step, and the code matches an earlier version that runs.

I also tried the timer scenario, to no avail... and the only way frmStudents is called is in a line subsequent to the modal loading of form frmEnterSchool.

This is an app that has been in production and in continual development/enhancement for a couple of years.... The only difference is that I have added some forms. Is it possible that I have too many forms? Stepping through the code, it executes perfectly, but running it causes the frmEnterSchool form to load, then the frmStudents form to begin loading without regard to the modality of frmEnterSchool.


I TRULY appreciate your collective brainpower :)
 
Is there a parent-child relationship between any of the forms in question? Anything is possible, the problem is I only have one lifetime.
[cheers]
 
try moving this line

frmMain.fBol_LoadingForm = False

from the startup timer to form_unload event of frmEnterSchool.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
I dont see whey you are supplying the 'Me' as the second parameter of the Show event. The only point to this parameter is if you want to keep a form 'always on top' to another. This is irrelevant if the form in quetion is modal. Remove the second parameter and see what happens.
 
If somewhere in the load or activate events of the frmEnterSchool form a Hide or Unload exists, or in a proceedure that is called from one of these events, then when the form hides or unloads, the code in the calling proceedure following the call frmEnterSchool.Show vbModal will execute.
A Modal form will retain the focus and code following the Show method will not execute until the Modal form is Unloaded OR hidden.

So, to start with, put a stopper on every Hide, Unload, or similar actions in frmEnterSchool, and see if code execution flow runs into one of these right after frmEnterSchool.Show.

(There may be code somewhere in a LostFocus or Change event that is getting called later(or earlier) than expected putting a stopper may prevent it from being called at all or it may get called earlier than expected).

 
I found a fix. I took an older version of the form that did work, and opened it and the new one in a text editor (to include the form description in text). Then I cut and paste from the new to the old, testing each change by running it in VB. Finally, I added the code that describes my latest menubar addition, "Tasks", with subitems "Task Manager" and "My Task List", and it choked. I took the menuitems out and it ran, I put them back with different names, and it still choked.

I don't know why this would cause a 401 error, but without these menuitems, I get no error. So I'll add a button to the toolbar to supply the "Task" functionality, and ditch the offending menu...

Thanks a million for all the thoughtful advice!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top