Form 对象 (Access)

Form 对象引用一个特定的 Microsoft Access 窗体。

备注

Form 对象是 Forms 集合的一个成员,后者是当前打开的所有窗体的集合。 在 Forms 集合中,各个表单从零开始编制索引。 通过按名称引用表单或引用其在集合中的索引,引用 Forms 集合中的单个 表单 对象。

若要引用 Forms 集合中的特定表单,最好按名称引用表单,因为表单的集合索引可能会发生变化。 如果表单名称包含空格,必须使用方括号 ([ ]) 将名称括起来。

语法 示例
AllForms!formname AllForms!OrderForm
AllForms![form name] AllForms![Order Form]
AllForms("formname") AllForms("OrderForm")
AllForms(index) AllForms(0)

每个 Form 对象都有一个 Controls 集合,该集合包含窗体上的所有控件。 通过隐式或显式引用 控件 集合来引用表单上的控件。 如果隐式引用 Controls 集合,代码运行速度更快。 下例显示了可用于在名为 OrderForm 的窗体上引用 NewData 控件的两种方法。

 ' Implicit reference. 
Forms!OrderForm!NewData
' Explicit reference. 
Forms!OrderForm.Controls!NewData

下面的两个示例显示了如何引用子窗体 ctlSubForm 上名为 NewData 的控件(该子窗体包含在名为 OrderForm 的窗体中)。

Forms!OrderForm.ctlSubForm.Form!Controls.NewData
Forms!OrderForm.ctlSubForm!NewData

示例

下面的示例演示如何使用 TextBox 控件,以提供查询的日期条件。

Private Sub cmdSearch_Click()

   Dim db As DAO.Database
   Dim qd As QueryDef
   Dim vWhere As Variant

   Set db = CurrentDb()

   On Error Resume Next
   db.QueryDefs.Delete "Query1"
   On Error GoTo 0

   vWhere = Null

   vWhere = vWhere & " AND [PayeeID]=" + Me.cboPayeeID

   If Nz(Me.txtEndDate, "") <> "" And Nz(Me.txtStartDate, "") <> "" Then
      vWhere = vWhere & " AND [RefundProcessed] Between #" & _
      Me.txtStartDate & "# AND #" & Me.txtEndDate & "#"
   Else
      If Nz(Me.txtEndDate, "") = "" And Nz(Me.txtStartDate, "") <> "" Then
         vWhere = vWhere & " AND [RefundProcessed]>=#" _
                  + Me.txtStartDate & "#"
      Else
         If Nz(Me.txtEndDate, "") <> "" And Nz(Me.txtStartDate, "") = "" Then
            vWhere = vWhere & " AND [RefundProcessed] <=#" _
                     + Me.txtEndDate & "#"
      End If
     End If
   End If

   If Nz(vWhere, "") = "" Then
      MsgBox "There are no search criteria selected." & vbCrLf & vbCrLf & _
             "Search Cancelled.", vbInformation, "Search Canceled."
   Else
      Set qd = db.CreateQueryDef("Query1", "SELECT * FROM tblRefundData? & _
               " WHERE " & Mid(vWhere, 6))
      db.Close
      Set db = Nothing

      DoCmd.OpenQuery "Query1", acViewNormal, acReadOnly
   End If
End Sub

下面的示例展示了如何使用表单的 BeforeUpdate 事件,要求在一个控件中输入值(如果另一个控件也有数据的话)。

Private Sub Form_BeforeUpdate(Cancel As Integer)
If (IsNull(Me.FieldOne)) Or (Me.FieldOne.Value =  "") Then
    ' No action required
Else
    If (IsNull(Me.FieldTwo)) or (Me.FieldTwo.Value = "") Then
        MsgBox "You must provide data for field 'FieldTwo', " & _
            "if a value is entered in FieldOne", _
            vbOKOnly, "Required Field"
        Me.FieldTwo.SetFocus
        Cancel = True
        Exit Sub
    End If
End If

End Sub

下面的示例展示了如何使用 OpenArgs 属性阻止从导航窗格打开窗体。

Private Sub Form_Open(Cancel As Integer)

If Me.OpenArgs() <> "Valid User" Then
    MsgBox "You are not authorized to use this form!", _
        vbExclamation + vbOKOnly, "Invalid Access"
    Cancel = True
End If
End Sub

下面的示例演示如何使用 OpenForm方法的 WhereCondition 参数筛选在已打开的窗体上显示的记录。

Private Sub cmdShowOrders_Click()
If Not Me.NewRecord Then
    DoCmd.OpenForm "frmOrder", _
        WhereCondition:="CustomerID=" & Me.txtCustomerID
End If
End Sub

事件

方法

属性

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。