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

Can a form kill itself?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a modal form which can become terribly recursive once it's accomplished its task. The code is structured in such a way that it will keep going forever if I can't stop it, which I don't seem to be able to.

It can recognize when it's finished the task I've asked it to do, though. Is there a way I can have the form "commit suicide" so it stops executing its code at this point? I've tried things like hide and unload, but the code keeps going and going and going...
 
Thanks John, but I seem to have entered this code wrong or something.

I entered the UNLOAD ME statement at the point in the modal form's code where I wanted it to die.

I then watched the form run through the unload_form procedure, where it encountered the line

Set frmname = Nothing

After that, it continued merrily on its way through the rest of the code, ad infinitum. This is VB 5 I'm using, if that makes a difference.
 
Sounds like you might have the UnLoad statement in a Do Loop. If so, put a DoEvents statement right after the Unload statement, or an Exit Do statement, etc...
 
Thanks Tim. I think if I explain this project in further detail, you may understand what is going on.

This form is part of a larger program that simulates a hockey league. The form is the part that plays the game. It has subs for many of the various situations that might arise over the course of a game, such as:

faceoff_neutral_zone
possession_off_zone
loose_neutral_zone

etc. Now what happens, is that the execution of the form is redirected back and forth throughout these subs until the timer tells it that a period has expired. Once three periods have expired, it goes to the end_game sub, where I want it to stop and unload, returning control to the form that spawned it modally.

I think the problem is that most of these subs never reach their end sub command. Each sub travels directly to one of several other subs, depending on random determination. It seems to me to be the most logical way to simulate a hockey game, but VB doesn't seem to like it.
 
By the way, I think one way to prevent this recursive execution is to use a variable in a master call procedure. Rather than entering new subs (say, the faceoff sub from the possession sub or whathaveyou) immediately, I can let the sub reach its end sub, where it returns to a master sub that then dynamically calls the next sub using the variable.

This would require that

a. VB lets me call subs using strings (I don't know)
b. I rewrite all of the code to suite this new method of execution, which I will do only if I have to.
 
Alright, I entered a bunch of exit subs after all of the calls in the form. Now it systematically traces its roots back until it's exited all of the subs and ends up back at the form_load procedure where it started.

Only problem now is, the form.hide command doesn't transfer control back to the form that spawned this modal form. The only way to get that code going again is to click the little "x" and manually close the modal form.

This seems strange, because the form.hide works on my other modal forms. I'm sure it's something simple, and please pardon my ignorance in these matters, but I am as yet a neophyte programmer.
 
What are doing the Timers ?? ( are the stopped ,timer1.interval 0 or timer.enabled =false ??)

Sorry ,whit out of eany code i can not see whats happen or wrong

a nice day


Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Thanks EdEric. I'm not using any timers for anything. But here's the code that calls the modal form frmgame from frmtoday, the master form, along with the code in the frmgame_load procedure that ought to transfer control back to frmtoday:

from frmtoday:

Private Sub cmdplay_Click()

rcstoday.MoveFirst


rcstodayhome.Seek "=", rcstoday!home
rcstodayvisitor.Seek "=", rcstoday!Visitor
frmgame.Show 1, Me

frmtoday.Refresh
rcstoday.MoveNext

End Sub

here's the code in frmgame that ought to transfer control back to frmtoday. Instead, it doesn't do that, and frmtoday just sits there on the line frmgame.show until I manually close frmgame. If I put an unload statement in frmgame, I get an error when it returns to the frmgame.show line that VB doesn't recognize:

Private Sub Form_Load()


Randomize
home = 0
away = 1
ReDim Preserve scoring(0)
play_game

frmgame.Hide

End Sub
 
Well, thanks everyone for your help, but it's still no go.

I added the show statement like you suggested, edderic, but frmgame still sits there and frmtoday stays dormant. Maybe its because frmgame requires no input from the user? Or maybe it's because all of frmgames variables have values in them? At any rate, something screwy is going on. Maybe I'll try to reprogram the whole thing and make everything cleaner, and... well, who knows, we'll just have to see what happens.

Live and learn! Or live and wonder just what the heck went wrong!
 
a modal form is a special kind of form that retains the focus until it's closed. After you have exitied from all the subs use Unload Me IN THE MODAL FORMS CODE. control will not return to the main form until you close the modal form. Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
To solve this problem :

take a timer on frmgame and set interval = 1
in the timer next code : unload Me

After :

Randomize
home = 0
away = 1
ReDim Preserve scoring(0)
play_game

In de frmgame_Unload
timer1.enabled =false
timer1.Interval =0

I hope this help you


Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
I'm puzzled by your code: You seem to Load the frmGame form to do some processing and to ... Hide it. (frmgame.Hide in the Load event of frmGame). Is it the intention that somebody sees this form? And if not, why do you use a form?

A possible origin of the problem could be that you don't seem to have, as far as i can see in your code, a separate Load statement for the frmGame form. The Load is probably performed embedded in the Show statement.

I did separate the two (Load & Show) in the following piece of code, which tries to mimic the essentials(?) of your problem. No loop occurs and no multiple copies of frmGame show up when clicking the command button.

'Code for frmToDay
Private Sub Form_Load()
Load frmGame
End Sub

Private Sub cmdPlay_Click()
frmGame.Show vbModal, Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
Unload frmGame
Set frmGame = Nothing
Unload Me
Set frmToday = Nothing
End Sub


'Code for frmGame
Private Sub Form_Activate()
Me.Hide
End Sub


Ruairi: ...a modal form is a special kind of form that retains the focus until it's closed. ... Control will not return to the main form until you close the modal form.

Or until you Hide it: The program must Hide or Unload a modal form before input to another form can occur. (MS VB6 Lang. Ref; P965)



_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Wow! I go away for a couple of days thinking that's the end of that, and I come back to find all of this advice here!

You'll have to give me a moment to try all of this stuff, but thanks for your help everyone.

rvbasic, your advice seems particularly helpful. I am loading frmgame and then hiding it when it's done its processing. I need to spawn it multiple times in succession from frmtoday in order to play several games in a row. Forgive the ignorance of someone relatively new to vb, but should this code go somewhere other than a form? The frmgame.hide statement is intended to unload the form, which it doesn't do at this point.

A modal form seems ideal in this situation, because then frmtoday will wait until the game has been processed before sending the next game for processing. Ideally.

Well, I'm going to try some of these, and I'll get back to you folks with the results. Thanks again!
 
What a long thread! I know I've probably said it enough already, but thanks for all the help everyone.

rvbasic, your code made everything work just fine. I had tried just about everything in there, but I had never loaded frmgame from frmtoday. As you say, it was just done implicitly with the form.show statement. I guess you have to load separately to make this work.

Well, until I reach my next stumbling block (actually, things have gone pretty well so far),

Au revoir
 
you are Frenschman ? (Or Belgium Frenschman ?)

Take the timer option and it works



Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
edderic;

I was going to try the timer code next, but I didn't need to because the first thing I tried worked. Also, I've never used the timer before, and I'm going to put off learning to use it until I need to.

I'm not French, though I studied it for many years. I'm Canadian. You, I assume, are Dutch? I know an Eric from Holland. In fact, he's visiting here in Canada right now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top