Wed. May 22nd, 2024
Microsoft Access VBA Open Form to a New or Specific Record
Microsoft Access VBA Open Form to a New or Specific Record

The VBA code is the feature in Ms Access for programming to adding more function to Access database, using Visual Basic. To open the VBA window, press the Alt + F11 buttons in your keyboard. The Open Form function is to open specific form when an action is occurred, such as when clicking button in a form. Even through the feature can be achieved with command button wizard, sometimes you may need to use it in VBA for more complex or specified result.

The general syntax for Open Form function:
DoCmd. OpenForm (FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs)


FormName: the string contains name of the form. It is the only required parameter, the rest of the parameter below is optional.

View: determine how the form will be opened and presented. Values: acNormal (normal form), and acFormDS (datasheet).

FilterName: string that refers to the query name in the database

WhereCondition: it is same like SQL Where function. It is to extract specific result from the database.

DataMode: about how the data entry mode for the form. There are 3 values: acFormAdd (opens the form on a ‘new’ (empty) form, acFormEdit (allows editing), and acFormReadOnly (no editing allowed).

WindowMode: determine how the window mode in which the form opens, like pop up or not. The value are: acWindowNormal (Normal view), acDialog (dialog box), and PopUp.

Read Also:  Ms Access Union Query SQL Function Syntax

OpenArgs: the function to pass data to the form. The OpenArgs function can be used for several things, such as creating Modules, or procedure before opening the form. Sometimes, the OpenArgs is become the function instead the parameter for OpenForm, even though it still need to be used with OpenForm.

1. DoCmd. OpenForm (“MemberDetails”, acNormal, , , acFormEdit, PopUp)
This will open the MemberDetails form in Normal Mode, that can be edited and is a PopUp form.

Microsoft Access VBA Open Form 1
Microsoft Access VBA Open Form 1

2. DoCmd.OpenForm “BooksDetails”, , , , “FirstName = ’Danang’”
This will open the BooksDetails form, with condition only for Danang.

3. Example in Library Database

Microsoft Access VBA Open Form 2
Microsoft Access VBA Open Form 2

It will open the BookForm form, with CategoryID is BR. Read more tutorial for Microsoft Access VBA.