To open a form in VBA, use the DoCmd.OpenForm method. The simplest line is DoCmd.OpenForm "FormName", which opens the form in normal view. You can add arguments to filter records, restrict edits, or open in dialog mode. This guide covers the exact syntax, where to put the code, and what to do when it fails.

What You'll Need

  • Microsoft Access (any modern version) with a database containing at least one form.
  • Basic familiarity with the VBA editor (press Alt+F11 to open it).
  • The exact name of the form you want to open — check the Navigation Pane.

Step-by-Step

  1. Open the VBA editor. In Access, press Alt+F11. The editor opens in a separate window.
  2. Decide where the code will run. You can put it in a standard module (Insert > Module) for general use, or behind a button on a form (in that form's code module). For a quick test, use the Immediate Window (Ctrl+G) and type the command directly.
  3. Write the basic command. In the Immediate Window, type:
    DoCmd.OpenForm "YourFormName"
    Replace YourFormName with the actual name of your form, including spaces if any. Press Enter. The form should open immediately.
  4. Use it in a procedure. For reusable code, create a Sub in a standard module:
    Sub OpenMyForm()
        DoCmd.OpenForm "YourFormName"
    End Sub
    Run it with F5 or from the Immediate Window by typing OpenMyForm.
  5. Add optional arguments. The full syntax is:
    DoCmd.OpenForm FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs
    All arguments after FormName are optional. For example, to open a form filtered to a specific customer ID:
    DoCmd.OpenForm "frmCustomers", , , "CustomerID = 123"
    To open in dialog mode (modal, blocking other windows):
    DoCmd.OpenForm "frmCustomers", , , , , acDialog
  6. Handle errors. If the form name is wrong or missing, VBA raises error 2102 or 2501. Wrap the call in error handling:
    On Error GoTo ErrHandler
    DoCmd.OpenForm "YourFormName"
    Exit Sub
    ErrHandler:
    MsgBox "Could not open form: " & Err.Description
  7. Test from a button. On a form, add a command button (from the toolbox), set its On Click event to [Event Procedure], then in the VBA code write the DoCmd.OpenForm line. Switch to Form View and click the button.

Note: Menu labels and exact argument names can differ slightly between Access versions and localizations. The core method DoCmd.OpenForm is stable across versions.

Common Mistakes

  • Misspelling the form name. VBA is case-insensitive for names but requires exact spelling. Use the dropdown in the VBA editor to avoid typos.
  • Forgetting quotes around the form name. DoCmd.OpenForm MyForm without quotes treats MyForm as a variable, causing a compile error.
  • Using the wrong argument order. If you skip an argument, you must still include the comma. For example, to set only the WhereCondition, you need three commas before it: DoCmd.OpenForm "Form1", , , "ID=1".
  • Assuming the form opens in front. If another window has focus, the form may open behind it. Use acDialog or set focus after opening.
  • Placing code in the wrong module. Code in a form's module can only run when that form is open. For global macros, use a standard module.

Tips

  • Use acDialog for pop-up forms that require user input before continuing.
  • Pass OpenArgs to send data to the form being opened; retrieve it in the form's Open event with Me.OpenArgs.
  • To open a form and filter it based on a control on the current form, build the WhereCondition string dynamically, e.g., "CustomerID = " & Me!txtCustomerID.
  • Use acFormAdd in the DataMode argument to open the form ready for a new record.
  • Always include error handling — a missing form or bad filter will otherwise show a cryptic error.

Troubleshooting

If the form does not open, check these in order:

  1. Compile error? The line is highlighted in red. Check quotes and argument commas.
  2. Run-time error 2102: "The form name 'X' is misspelled or refers to a form that doesn't exist." Verify the form name in the Navigation Pane. Also ensure the form is saved and not in design view with unsaved changes.
  3. Run-time error 2501: The OpenForm action was canceled. This often happens if the form's Open event cancels itself (e.g., via Cancel = True). Check the form's code.
  4. Nothing happens? The code may not be running. Add a MsgBox "Opening..." before the OpenForm line to confirm execution.
  5. Form opens but is empty? Your WhereCondition may be filtering out all records. Test without the filter first.
  6. Form opens behind other windows? Use acDialog or call DoCmd.SelectObject acForm, "FormName" after opening.

If none of that works, create a new blank form and try opening it with the same code. If that succeeds, the issue is with the original form's properties or events.

Frequently Asked Questions

Can I open a form from another form's button?

Yes. In the button's On Click event procedure, use DoCmd.OpenForm "TargetFormName".

How do I open a form and go to a specific record?

Use the WhereCondition argument: DoCmd.OpenForm "frmContacts", , , "ContactID = 42".

What is the difference between acNormal and acDialog?

acNormal opens the form as a normal window; acDialog opens it as a modal dialog that must be closed before the user can interact with other windows.

Can I open a form in datasheet view?

Yes, set the View argument to acFormDS: DoCmd.OpenForm "frmOrders", acFormDS.

Why does my form open but immediately close?

Check the form's Load or Open event for code that closes it, or a macro that runs on open. Also ensure no error is triggering a close.

Is DoCmd.OpenForm available in Access 2007 and later?

Yes, it has been part of Access VBA for many versions and works in all modern Access releases.