Can't close and delete a form (1 Viewer)

CedarTree

Registered User.
Local time
Today, 16:20
Joined
Mar 2, 2018
Messages
404
Hello. I have a MAIN form that needs to differ depending on the situation. So I have two TEMPLATE forms that I want to use to replace the MAIN form.
So roughly the logic is this:
  • Close the MAIN form
  • Delete the MAIN form
  • Copy TEMPLATE_A or TEMPLATE_B and rename them into MAIN form
The problem is that after closing the MAIN form (to then delete it), Access gives me error 2008 and it seems it thinks the MAIN form is still open???

Here's my current code:
Code:
    DoCmd.SetWarnings False
    DoCmd.Close acForm, "usysfrmMain"
    DoCmd.DeleteObject acForm, "usysfrmMain"
    DoCmd.SetWarnings True
 
Last edited:

Ranman256

Well-known member
Local time
Today, 16:20
Joined
Apr 9, 2015
Messages
4,337
once you close the form, the code in the form evaporates and shouldnt be able to run.
make the last step CLOSE FORM
 

CedarTree

Registered User.
Local time
Today, 16:20
Joined
Mar 2, 2018
Messages
404
I'm actually running that code from a general module. And I've tested that the form is not Loaded before I run this code. Would subforms be a culprit?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 21:20
Joined
Feb 19, 2013
Messages
16,639
remove or comment out your setwarnings until you have resolved the issue.

Closing a form can take a moment or two so it may be your deleteobject is 'too soon'

try putting

doEvents

between closing and deleting the form

And I've tested that the form is not Loaded before I run this code
so why are you trying to close it?

Would subforms be a culprit?
only in terms of extending the time it takes to close the form

Also not sure if you need to delete the old main form before renaming one of the templates - do that in the navigation window and you are prompted if you want to replace the existing form, might be worth considering as an alternative method
 

CedarTree

Registered User.
Local time
Today, 16:20
Joined
Mar 2, 2018
Messages
404
What's weird is that IsLoaded = false, but it won't let me delete it, even manually!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:20
Joined
Feb 28, 2001
Messages
27,257
I'm actually running that code from a general module. And I've tested that the form is not Loaded before I run this code. Would subforms be a culprit?

This is maybe a simple-minded response, but if the general module code you are calling is being called from the form you are trying to delete, you can't do that because the form is open and busy. In order to do what you described, there must be at least one other form (or a macro with a run-code action) to drive this process.

Separately, as a general rule you NEVER disable warnings from code in the process of being debugged. You only close off error messages when you have good reason to believe there won't be any.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:20
Joined
Feb 19, 2002
Messages
43,392
So I have two TEMPLATE forms that I want to use to replace the MAIN form.
There is something very wrong with your design if you think this is necessary. Why would you not simply open version 1 or version 2?
 

Users who are viewing this thread

Top Bottom