Close and Delete Form on On_Load Event

BigJimSlade

Registered User.
Local time
Today, 22:44
Joined
Oct 11, 2000
Messages
173
Hi, Big Jim here:

This is what I have. Form A and Form B...

In Form A's On_Load Event, Form B is opened....

Form B's On_Load Event wants to close and then Delete Form A.


I get an error message telling me that Form A is still open ( I assume because code has not run completely). Is there anyway to get this to work?

Thanks in advance!

Big Jim
 
Change the code on FormB to Close FormA before deleting:

DoCmd.Close acForm, "FormA"

Then the delete code...
 
Hi jf,

That is what I am doing. I am running a docmd to close the form and then i am running a docmd to delete the form, but it will not let me delete the form.

Thanks

big Jim
 
Big Jim:

Can you copy your code so that I ca review it?
 
'MacroGenerator opens another form called MacroGenerator2. This is in the on_load event:



DoCmd.Close acForm, "MacroGenerator", acSaveNo
DoCmd.Close acForm, "MacroGenerator", acSaveNo



Thanks for your help!

Big Jim
 
I thought of the same thing, but the Timer event deletes the original formand then tries to delete it again. The you get a Cannot find xxx message box.
 
Thanks for all of the suggestions! I think my problem was that since my second form was being opened from the first one, and the deleting of the first form wsa occuring in the on_load event of the second form, the process would not complete as long as the code was technically still running in the first form.

Does that make any sense?

Thanks!

Big Jim
 
Try putting the code in the OnActivate event instead, when a form loads these events occur in this order:

FORM EVENTS: Open>Load>Resize>Activate>Current>
CONTROL EVENTS: GotFocus>Enter

By using exclusionary code provided by Rich you can delete the form in the activate event without causing an error when the Activate event is retriggered.
 
Fornation, Big Jim here:

I appreciate your thoughts on the matter, but I am still having problems. Here is sample I tried to run with no luck:

Forms: Test1, Test2

Test1 has 1 button on it. When I click the button, Test2 opens. Test2 runs code (in the On_Activate Event) to delete Test1. I still get the error that Test1 cannot be deleted while still open. Here is my code for Test1:

Private Sub Command1_Click()
DoCmd.OpenForm "Test2"
End Sub


Test 2 Code:

Private Sub Form_Activate()
If Not IsLoaded("Test1") Then
Else:
DoCmd.DeleteObject acForm, "Test1"
End If
End Sub

Thanks!
Big Jim

PS...for anyone else who is trying to use this code, the function "IsLoaded" is not an intrinsic function in Access. It must be imported from the Northwind Sample Database.
 
hmmm...

have you tried using a third form as a transaction form?
open form2 from form1, then open form3 from form2's open event and delete form1 from form3's open event.
 
Sussed it!

use the timer event as Rich suggests, I set mine at 1000 and used a boolean variable to test whether I'd already deleted the form.

Dim blnRunOnceAlready As Boolean

Private Sub Form_Timer()
If Not(blnRunOnceAlready)Then
DoCmd.DeleteObject acForm, "form1"
blnRunOnceAlready = True
End If
End Sub
 
Thanks Fornatian. That is actually exactly what we did. I was hoping that there was an easier alternative that wouldn't require the import of a third form onto the user's computer, but it is all good.


Thanks again!

Big Jim
 
Big Jim :D ,

There is no need for a third form, you can run the timer code through the second form - I have tested it. The only code you need is this:


Private Sub Form_Timer()

'delete the object

DoCmd.DeleteObject acForm, "form1"

'set timerinterval to 0 so it doesn't keep triggering

Me.TimerInterval = 0

End Sub

As long as the timerinterval for form2 is set at say 1000 then form1 can clear from memory in time for the timer code to run without overlap.
 
jfgambit!

Very ingenious! Big Jim is impressed with your knowledge, and he thanks you for all of your help in this thread. Thanks to everyone, and I hope this helps some other people out too!

Big Jim
 

Users who are viewing this thread

Back
Top Bottom