Access VBA – Open / Close Form

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 9, 2022

In this tutorial, we will learn how to open or close Access forms using VBA.

Opening an Access Form

To open a form in Access, use the DoCmd.OpenForm method:

DoCmd.OpenForm "AccessForm"

This will open the Form named “AccessForm”:

Navigation Pane in Access

AccessForm Shown in Navigation Pane

Open Form With Criteria

This code will open a form in Access with specific criteria:

DoCmd.OpenForm "AccessForm", acNormal, , "ID=10"

DoCmd.OpenForm Syntax

The full syntax of the method is:

DoCmd.OpenForm(NameOfForm, FormView, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs) where:

Parameter Description
NameOfForm Required and is the name of the form you want to open.
FormView Optional and allows you to specify the view of the form. This can either be: acDesign, acFormDS, acFormPivotChart, acFormPivotTable, acLayout, acNormal, acLayout, or acPreview. The default is acNormal.
FilterName Optional and allows you to specify the name of a query or SQL string to use as a filter.
WhereCondition Optional and allows you to conduct a where type query without using the word where.
DataMode Optional and allows you to specify your data entry mode. This can either be: acFormAdd, acFormEdit, acFormPropertySettings, or acFormReadOnly. The default is acFormPropertySettings.
WindowMode Optional and sets the display of the Window mode. This can either be: acDialog, acHidden, acIcon, acWindowNormal. The default is acWindowNormal.
OpenArgs Optional can be referred to in macros and expressions.

Close a Form in Access

Use DoCmd.Close to close an open form:

DoCmd.Close acForm, "AccessForm"

Close Form and Save

This VBA code will close and save an Access form:

DoCmd.Close acForm, "AccessForm", acSaveYes

Prompt Before Closing Form

This procedure will prompt the user before closing a form:

Public Sub CloseFormWithConfirmation(FormName As String)
    If MsgBox("Are you sure you want to close this window?", vbYesNo + vbQuestion, "Confirmation") = vbYes Then
        DoCmd.Close acForm, FormName
    End If
End Sub

You can call it like this:

Private Sub CloseFormWithConfirmation_Example()
    Call CloseFormWithConfirmation("AccessForm")
End Sub

DoCmd.Close Syntax

The full syntax of the method is:

DoCmd.Close (ObjectType, ObjectName, ObjectSave) where:

Parameter Description
ObjectType Optional and this is the type of object that you would like to close. This can be a form or report etc.
ObjectName Optional and is the name of the object that you want to close.
ObjectSave Optional and is used to specify whether you want to save the changes made.

 

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples