2015年3月6日 星期五

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)















FormNameName of Form (in String)
ViewOptional. How you want to view the Form
























NameValueDescription
acDesign1The form opens in Design view.
acFormDS3The form opens in Datasheet view.
acFormPivotChart5The form opens in PivotChart view.
acFormPivotTable4The form opens in PivotTable view.
acLayout6The form opens in Layout view.
acNormal0(Default) The form opens in Form view.
acPreview2The form opens in Print Preview.
FilterNameInput SQL String or existing Query name to use its Where Clause (but not Record Source)
WhereConditionInput Where Clause in String (without Where keyword)
DataModeOptional. How you want the input mode to be in Form view or Datasheet view















NameValueDescription
acFormAdd0The user can add new records but can’t edit existing records.
acFormEdit1The user can edit existing records and add new records.
acFormPropertySettings-1The user can only change the form’s properties. (Default)
acFormReadOnly2The user can only view records.
WindowModeOptional. Set the display Windows mode















NameValueDescription
acDialog3The form or report’s Modal and PopUp properties are set to Yes.
acHidden1The form or report is hidden.
acIcon2The form or report opens minimized in the Windows taskbar.
acWindowNormal0(Default) The form or report opens in the mode set by its properties.
OpenArgsOptional. 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 Access DoCmd.OpenForm Method – open Access Form


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


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


DoCmd.OpenForm "Result_Form", , "tempQry"

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



Access DoCmd.OpenForm Method to open Access Form

沒有留言:

張貼留言