Close a form, report or window.

      DoCmd.Close(ObjectType, ObjectName, Save)

   ObjectType   An AcObjectType constant that represents the type of object to close.

   ObjectName   A string expression that's the valid name of
                an object of the type selected by the objecttype argument.

   Save         Whether or not to save changes to the object:
                  acSavePrompt (0)
                  acSaveYes (1)
                  acSaveNo (2)

The Close method can be used to close either a specified Access window or the active window (if ObjectType and ObjectName are left blank).

The Save options will save any design changes that have been made to the form/report, this option does not save the record data.

If a form is closed without first saving the record, Access will attempt to save the record but this can fail - if a required field is missing any changes made to the record will be cancelled and no error will be displayed.

A good practice is to use RunCommand acCmdSaveRecord immediately before calling .Close. This will cause a run-time error if one or more required fields are Null. This is illustrated in the following example.


'Save the current record and close the current window
On Error GoTo Err_handler
DoCmd.RunCommand acCmdSaveRecord

'Close the form frmPatientDetails, without saving any form design changes:
DoCmd.Close acForm, "frmPatientDetails", acSaveNo

'Close the active object, without saving any form design changes:
DoCmd.Close , , acSaveNo

A function to close any form without saving design changes

Public Sub CloseMe(frmMe As Form)
  DoCmd.Close ObjectType:=acForm, ObjectName:= frmMe.Name, Save:=acSaveNo
End Sub

' Call this with CloseMe Me

“A good opening and a good ending make for a good film provide they come close together” ~ Federico Fellini


Maximize - Enlarge the active window.
Minimize - Minimise a window.
RecordSet.Close - Close a recordset

Copyright © 1999-2024
Some rights reserved