DoCmd.Close method (Access)

The Close method carries out the Close action in Visual Basic.

Syntax

expression.Close (ObjectType, ObjectName, Save)

expression A variable that represents a DoCmd object.

Parameters

Name Required/Optional Data type Description
ObjectType Optional AcObjectType An AcObjectType constant that represents the type of object to close.
ObjectName Optional Variant A string expression that's the valid name of an object of the type selected by the ObjectType argument.
Save Optional AcCloseSave An AcCloseSave constant that specifies whether to save changes to the object. The default value is acSavePrompt.

Remarks

You can use the Close method to close either a specified Microsoft Access window or the active window if none is specified.

If you leave the ObjectType and ObjectName arguments blank (the default constant, acDefault, is assumed for ObjectType), Access closes the active window. If you specify the Save argument and leave the ObjectType and ObjectName arguments blank, you must include the ObjectType and ObjectName arguments' commas.

Note

If a form has a control bound to a field that has its Required property set to Yes, and the form is closed by using the Close method without entering any data for that field, an error message is not displayed. Any changes made to the record will be aborted. When the form is closed by using the user interface, Access displays an alert.

To display an error message, use the RunCommand method to invoke the acCmdSaveRecord command before calling the Close method. This will cause a run-time error if one or more required fields are Null. This technique is illustrated in the following example.

Private Sub cmdCloseForm_Click() 
On Error GoTo Err_cmdCloseForm_Click 
 
 DoCmd.RunCommand acCmdSaveRecord 
 DoCmd.Close 
 
Exit_cmdCloseForm_Click: 
 Exit Sub 
 
Err_cmdCloseForm_Click: 
 MsgBox Err.Description 
 Resume Exit_cmdCloseForm_Click 
 
End Sub

Example

The following example uses the Close method to close the form Order Review, saving any changes to the form without prompting.

DoCmd.Close acForm, "Order Review", acSaveYes

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.