docmd.close acform (main and subform?)

kate10123

Registered User.
Local time
Today, 15:18
Joined
Jul 31, 2008
Messages
185
Hi there,

I have a command button which is meant to take the user back to the main menu and close the current form but the normal command doesn't work because the form has a subform open as well.

The main form is called frm_tutor and the subform is called frm_tutorial1 but I don't know to format the following syntax to close both the subform and normal form.

Code:
   DoCmd.close acForm, "frm_tutor", acSaveYes
 
Hi Kate,
Let me guess, the DoCmd.Close is imbedded in the midst of the subform VB code. After the command executes, the control is passed back to the original VB module, and the next statement in that module cuases the subform or form to re-activate.
Place the DoCmd.Close at the end of the module and use a goto to get there, or place an End statement immediately after the docmd.
Smiles
Bob
 
Hi Bob
Thanks for replying!

No this is the only code I have on this button click event:

Code:
Private Sub Menu_Click()
DoCmd.OpenForm "frm_FrontTutorScreen"
DoCmd.close acForm, [B]frm_tutor!frm_tutorial1.Form[/B], acSaveYes
End Sub
 
LOL,
Switch the order,
Close
Open
Code:
Private Sub Menu_Click()
DoCmd.close
DoCmd.OpenForm "frm_FrontTutorScreen"
End Sub
Or
Code:
Private Sub Menu_Click()
DoCmd.close acForm, "[B]frm_tutor"[/B], acSaveYes

DoCmd.OpenForm "frm_FrontTutorScreen"
End Sub
 
Last edited:
By the way, the acSaveYes doesn't have anything to do with records (it has to do with design changes to the form) so you would normally want acSaveNo
 
Thanks for that, I assumed it was saving the record :)

If you just close the form the record is saved. But if you want to explicitly save a record at any time, the save code is one of two options:

DoCmd.RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False


The second actually only saves if there were changes to the bound form whereas the first will do a save even if nothing has changed.
 
It has long been recommended that the code

If Me.Dirty Then Me.Dirty = False

or

DoCmd.RunCommand acCmdSaveRecord

be inserted to save the record before using

DoCmd.Close

to close a form because of a quirk in Access. When DoCmd.Close is used, Access closes the form regardless of whether or not a PK field or other required field has been left blank or validation rule has been violated! If one of these things occur, Access will simply dump the record, close the form, and not tell the user that the record has been dumped!
 
Good point missinglinq.

What if you do not want a record saved?

DoCmd.CancelEvent (in the Before Update Event of the Form)

Then close form.

Lots of new code for you Kate....smiles.
 
Thanks a lot, will have to create a database to just store all these codes lol
 
I actually have a folder where I keep code samples and boilerplate responses for the more commonly asked questions. I currently have about 1600 items in the folder! The problem is that I'm an old code mechanic and getting older every day, and remembering what I called the file for a certain problem becomes more and more difficult!
 
That is a really good idea though, rather than having to open all different database files to find the code!
 

Users who are viewing this thread

Back
Top Bottom