DoCmd.Close Method

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 A 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 A AcCloseSave constant tha specifies whether or not 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), Microsoft 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 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 using the user interface, Microsoft 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.

Visual Basic for Applications
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:

Visual Basic for Applications
DoCmd.Close acForm, "Order Review", acSaveYes

Close close method shut a specific window using code use a method to shut a window Which method closes the active window?