This documentation is archived and is not being maintained.

Close Method [Access 2003 VBA Language Reference]

Office 2003

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

expression.Close(ObjectType, ObjectName, Save)

expression Required. An expression that returns one of the objects in the Applies To list.

AcObjectType can be one of these AcObjectType constants.
acDefault default

Note  If closing a module in the Visual Basic Editor (VBE), you must use acModule in the objecttype argument.

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

AcCloseSave can be one of these AcCloseSave constants.
acSavePrompt default This value is ignored if you are closing a Visual Basic module. The module will be closed, but changes to the module will not be saved.

If you leave this argument blank, the default constant (acSavePrompt) is assumed.


For more information on how the action and its arguments work, see the action topic.

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 Windows Close button, the Close action in a macro, or selecting Close from the File menu, 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.

Private Sub cmdCloseForm_Click()
On Error GoTo Err_cmdCloseForm_Click

    DoCmd.RunCommand acCmdSaveRecord

    Exit Sub

    MsgBox Err.Description
    Resume Exit_cmdCloseForm_Click
End Sub


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

Applies to | DoCmd Object