Closing a form without a 'save prompt'

Mark Rock

Registered User.
Local time
Yesterday, 19:07
Joined
Jan 5, 2007
Messages
16
I know this forum has a ton of posts related to closing a forum, but I have not had one workout for my purposes.

I have used CreateForm to open a new form from a template form. When I am done with it, I want it to go away and to do so quietly.

I have tried both

DoCmd.Close acForm, myfrm.Name, acSaveNo

and

DoCmd.RunCommand acCmdClose

The first works in that the form is no longer on top (it effectively returns focus to my module code), but it does not actually close the form. It is open in the background if I close/minimize the stuff on top of it. Seems to be of little difference to myfrm.Visible = False.

The second command works (almost) completely, but I have to click on a system invoked msgbox that I do not wish to save changes to the form before it goes away.

I would like to have the form close completely without asking me to save it.

Help please.
 
Last edited:
Woohoo! I am on a roll and seem to have solved my own issues today.

For anyone else wanting a simple way to do this try:

DoCmd.RunCommand acSaveNo

It closes the form and does not prompt to save it!!
 
Um. Oops.

It appears to work, but doesn't really. The form is still known to the database and when you go to close the database it remembers the form you created and asks you whether to save it. Still not acceptable.

Is there a way to remove it from the database at that point? Any hints appreciated.
 
Have you tried this:
Code:
DoCmd.Close acForm, myfrm.Name, acSaveNo
Set myfrm = Nothing

Since you are generating myfrm by instantiating a form object, you need to unload it in memory after closing or it will still actually be there.
 
Thank you for the reply Bob.

Since posting I had added that line. it still doesn't work. Crazy huh? How does it know the form name if the originating control is set to Nothing??

So far best tries have been,
Code:
DoCmd.Close acForm, myfrm.Name, acSaveNo
Set myfrm = Nothing

and

Code:
DoCmd.Close acForm, myfrm.Name, acSaveNo
DoCmd.RunCommand acSaveNo
Set myfrm = Nothing

Both of these make the form go away quietly -- but when I close the database it recalls that the form had existed and prompts me to save it.

I guess I'll spend today re-reading some of the posts on changing the exit message and see if that provides some hints on how to eliminate one!

Help still needed if someone has a further idea.
 
Couple of interesting observations from the latest deep-dive into OnClose events. OnClose does NOT get called when you issue DoCmd.Close acForm, myfrm.Name, acSaveNo

Instead it gets called whenever you actually get the Yes / No save prompt -- which may not occur until you exit the database. =)

True, but I am kind of being facitious (which is difficult because I doubt very much that I can even spell that), but perhaps that is a clue.

Another clue? Adding DoCmd.Quit acQuitSaveNone at the end of my module works perfectly. Of course it dumps me out of access altogether, but maybe I can live with that. I'd rather not if someone can improve on this.
 
In case someone else is looking for the same, I am posting that I finally found the solution to the issue I was having as described above. Attach the final code snippet at the end of your module. It never complains of mysterious open forms again!
:
Code:
DIM myfrm as Form
DIM cnt_controls as Integer

myfrm = CreateForm (....)
.
.
.


DoCmd.Close acForm, myfrm.Name, acSaveNo   ' may not be required, but I had it so I left it.
DoCmd.RunCommand acSaveNo                  ' may not be required, but again I had it so I left it.
Set myfrm = Nothing
cnt_controls = Forms.Count
Do While cnt_controls > 0
   DoCmd.DeleteObject acForm, Forms(cnt_controls - 1)
   cnt_controls = cnt_controls - 1
Loop

It deletes all instances of all open forms. Note my forms are unsaved and do not exist out of the module runtime, i.e. they are not saved. You would likely want to avoid using any references to allforms.count via something like currentproject.allforms.count and in the docmd deleteobject command itself as I believe that will remove both open and closed forms.
 
You can lose the DoCmd.RunCommand acSaveNo. You're clearing the "memory" (really the cache) with the Set myfrm = Nothing. That releases all existence of the form.

Note that you should do that with any instantiated object, all the time. For example, if you instantiate an Excel Object like this:

Code:
Dim xlApp As Excel.Application
.
.
Set xlApp = New Excel.Application
.
.
[COLOR="Red"]Set xlApp = Nothing[/COLOR]

Even something like a FileDialog should be handled in the same manner:

Code:
Dim dlg As FileDialog
.
.
Set dlg = Application.FileDialog(FileDialogType)
.
.
[COLOR="Red"]Set dlg = Nothing[/COLOR]

Essentially, anything you use a SET method with, you want to set it to Nothing at the end of the routine or function. Otherwise, you can potentially get some unexpected bizarre behavior. Besides, it's good anal retentive programming and clean code makes you look better than the guy that thinks modularization involves a trip to The Container Store. ;)
 

Users who are viewing this thread

Back
Top Bottom