Access DoCmd.OpenForm Method to open Access Form

This tutorial explains how to use Access DoCmd.OpenForm Method to open Access Form in Access VBA

Access DoCmd.OpenForm Method – open Access Form

DoCmd.OpenForm Method is used to open Access Form.

Syntax of Access DoCmd.OpenForm Method- open Access Form

DoCmd.OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs)
FormName Name of Form (in String)
View Optional. How you want to view the Form

Name Value Description
acDesign 1 The form opens in Design view.
acFormDS 3 The form opens in Datasheet view.
acFormPivotChart 5 The form opens in PivotChart view.
acFormPivotTable 4 The form opens in PivotTable view.
acLayout 6 The form opens in Layout view.
acNormal 0 (Default) The form opens in Form view.
acPreview 2 The form opens in Print Preview.
FilterName Input SQL String or existing Query name to use its Where Clause (but not Record Source)
WhereCondition Input Where Clause in String (without Where keyword)
DataMode Optional. How you want the input mode to be in Form view or Datasheet view

Name Value Description
acFormAdd 0 The user can add new records but can’t edit existing records.
acFormEdit 1 The user can edit existing records and add new records.
acFormPropertySettings -1 The user can only change the form’s properties. (Default)
acFormReadOnly 2 The user can only view records.
WindowMode Optional. Set the display Windows mode

Name Value Description
acDialog 3 The form or report’s Modal and PopUp properties are set to Yes.
acHidden 1 The form or report is hidden.
acIcon 2 The form or report opens minimized in the Windows taskbar.
acWindowNormal 0 (Default) The form or report opens in the mode set by its properties.
OpenArgs Optional. OpenArgs property can also be referred to in macros and expressions. For example, suppose that the form you open is a continuous-form list of clients. If you want the focus to move to a specific client record when the form opens, you can specify the client name with the OpenArgs argument, and then use the FindRecord method to move the focus to the record for the client with the specified name.

Example of using SQL String in FilterName argument

FilterName argument extracts the criteria in SQL / Query to use in Form Filter. If you only want to write the criteria to use it in Form, use WhereCondition argument (see below).

The following code defines a SQL string and use its criteria (not Record Source) in Result_Form when Result_Form is opened.

emplID_global = EmplID_tb
bookingDate_global = DateSerial(Year(bookingDate_tb), Day(bookingDate_tb), Month(bookingDate_tb))
searchCriteria = "[Empl ID] =" & "'" & emplID_global & "'" & " AND [Booking Date]=#" & bookingDate_global & "#"    
searchSQL_global = "Select * From [Schedule_Table] WHERE " & searchCriteria
DoCmd.OpenForm "Result_Form", , searchSQL_global

Example of using Query in FilterName argument

The following code uses criteria in Query (tempQry) in Result_Form when Result_Form is opened.

DoCmd.OpenForm "Result_Form", , "tempQry"

Example of using String in WhereCondition argument

The following code uses searchCriteria as criteria (statement after WHERE Clause) in Result_Form when Result_Form is opened.

emplID_global = EmplID_tb
bookingDate_global = DateSerial(Year(bookingDate_tb), Day(bookingDate_tb), Month(bookingDate_tb))
searchCriteria = "[Empl ID] =" & "'" & emplID_global & "'" & " AND [Booking Date]=#" & bookingDate_global & "#"    
DoCmd.OpenForm "Result_Form", , ,searchCriteria

Outbound References

https://msdn.microsoft.com/en-us/library/office/ff820845.aspx?f=255&MSPPError=-2147217396

Leave a Reply

Your email address will not be published.