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

Modal form. Subsequent Events Being Called 2

Status
Not open for further replies.

bobbarker

Programmer
Dec 20, 2001
83
0
0
GB
I have an MDIChild form with subsequent text boxes. On the lost focus event of each text box I perform a calculation and then call a procedure also on the form:

Private Sub Text1_LostFocus
'Do a calculation
CallSub
End Sub

Private Sub Text2_LostFocus
'Do a different calculation
CallSub
End Sub

The procedure called in the form ('CallSub') has error handling which calls a procedure called "Initialise" on a form called frmError (which is an MDIChild form):

Private Sub CallSub
On Error Goto CallSub_Err
'Do Something
Exit Sub
CallSub_Err:
frmError.Initialise Err, "Err Msg"
End Sub

This "Initialise" procedure on the Error form sets some objects and at the end:
Me.Show vbModal

Should an error occur in, for example, Text1_LostFocus the error form is called as expected. However, I am finding after Me.Show vbmodal is called in the frmError Initialise procedure, the Text2_LostFocus event is being called and again if an error happens it attempts to load frmError. When it gets to the Me.Show vbmodal I then get 'Run time error 400, Form already displayed, can't show modally'.

Bit of a newby I admit but I was hoping no further events would be called after the first Me.Show vbmodal until the user has closed the modally shown Error form.

Hope you can help.
 
Well, here's what I see happening:

You enter data into Text1, then tab to Text2 (which now has focus). This causes Text1 to lose focus, which calls the error checking and (if needed) displays frmError. When frmError is displayed, Text2 loses focus (to frmError), so the Text2 LostFocus event fires, which causes your problem.

I believe the best way to resolve this is to rethink your error handling. First, I wouldn't have the error checking done in the LostFocus event handler. Take a look at the Validate event and CausesValidation property - I think for what you are doing these will work much better.

Also, you may want tho do away with the modal form to handle errors - try putting your error handling code in a module and using message boxes to display error messages.

Hope this helps.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Thanks for the response - that is what I was suspecting. I'll try your suggestions

So my original assumption that no further events would be called outside of the modal form until it is unloaded or hidden is incorrect?
 
So my original assumption that no further events would be called outside of the modal form until it is unloaded or hidden is incorrect?

Yes, it is incorrect. You can illustrate this by making a project with 2 forms. Put a textbox and a timer on Form1 and set the timer's Enabled property to false, and give it an Interval of about 2000 or so. Paste in the following code:

Private Sub Form_Activate()
Text1.SetFocus
Timer1.Enabled = True
End Sub

Private Sub Text1_LostFocus()
MsgBox "Text1 Lost Focus."
End Sub

Private Sub Timer1_Timer()
Timer1.Enabled = False
Form2.Show vbModal
End Sub

Modal forms only stop code processing in the process they are called from. The LostFocus event is in a different process, and so it executes.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top