Form 物件 (Access)
Form 物件會參照特定的 Microsoft Access 表單。
Form 物件是 Forms 集合的成員,且該集合是所有目前開啟表單的集合。 在 Forms 集合內,會從零開始對個別表單編製索引。 透過依名稱參照表單,或參照其集合內的索引,以參照 [Form]集合中的個別 [Form]物件。
如果您想要參照在 Forms 集合中的特定表單,最好是因為在表單集合索引可能會變更依名稱參照至表單。 如果表單名稱包含空格,名稱必須用角括號 ([ ]) 框住。
語法 | 範例 |
---|---|
AllForms!formname | AllForms!OrderForm |
AllForms![form name] | AllForms![Order Form] |
AllForms("formname") | AllForms("OrderForm") |
AllForms(index) | AllForms(0) |
每個 Form 物件都有一個 Controls 集合,它包含了表單上的所有控制項。 透過隱含或明確地參照 [Controls]集合,以參照表單上的控制項。 如果您隱含地參照 Controls 集合,則程式碼會執行地更快速。 下列範例會顯示您可以參照在 OrderForm 表單上名為 NewData 的控制項的兩種方式。
' Implicit reference.
Forms!OrderForm!NewData
' Explicit reference.
Forms!OrderForm.Controls!NewData
下列兩個範例會顯示如何參照包含於 OrderForm 表單內的 ctlSubForm 子表單上名為 NewData 的控制項。
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
- Activate
- AfterDelConfirm
- AfterFinalRender
- AfterInsert
- AfterLayout
- AfterRender
- AfterUpdate
- ApplyFilter
- BeforeDelConfirm
- BeforeInsert
- BeforeQuery
- BeforeRender
- BeforeScreenTip
- BeforeUpdate
- Click
- Close
- CommandBeforeExecute
- CommandChecked
- CommandEnabled
- CommandExecute
- Current
- DataChange
- DataSetChange
- DblClick
- Deactivate
- Delete
- Dirty
- Error
- Filter
- GotFocus
- KeyDown
- KeyPress
- KeyUp
- 負載
- LostFocus
- MouseDown
- MouseMove
- MouseUp
- MouseWheel
- OnConnect
- OnDisconnect
- Open
- PivotTableChange
- Query
- Resize
- SelectionChange
- Timer
- Undo
- Unload
- ViewChange
- ActiveControl
- AfterDelConfirm
- AfterFinalRender
- AfterInsert
- AfterLayout
- AfterRender
- AfterUpdate
- AllowAdditions
- AllowDatasheetView
- AllowDeletions
- AllowEdits
- AllowFilters
- AllowFormView
- AllowLayoutView
- AllowPivotChartView
- AllowPivotTableView
- Application
- AutoCenter
- AutoResize
- BeforeDelConfirm
- BeforeInsert
- BeforeQuery
- BeforeRender
- BeforeScreenTip
- BeforeUpdate
- Bookmark
- BorderStyle
- Caption
- ChartSpace
- CloseButton
- CommandBeforeExecute
- CommandChecked
- CommandEnabled
- CommandExecute
- ControlBox
- Controls
- 數目
- CurrentRecord
- CurrentSectionLeft
- CurrentSectionTop
- CurrentView
- Cycle
- DataChange
- DataEntry
- DataSetChange
- DatasheetAlternateBackColor
- DatasheetBackColor
- DatasheetBorderLineStyle
- DatasheetCellsEffect
- DatasheetColumnHeaderUnderlineStyle
- DatasheetFontHeight
- DatasheetFontItalic
- DatasheetFontName
- DatasheetFontUnderline
- DatasheetFontWeight
- DatasheetForeColor
- DatasheetGridlinesBehavior
- DatasheetGridlinesColor
- DefaultControl
- DefaultView
- Dirty
- DisplayOnSharePointSite
- DividingLines
- FastLaserPrinting
- FetchDefaults
- Filter
- FilterOn
- FilterOnLoad
- FitToScreen
- Form
- FrozenColumns
- GridX
- GridY
- HasModule
- HelpContextId
- HelpFile
- HorizontalDatasheetGridlineStyle
- Hwnd
- InputParameters
- InsideHeight
- InsideWidth
- KeyPreview
- LayoutForPrint
- MaxRecButton
- MaxRecords
- MenuBar
- MinMaxButtons
- 強制回應
- 模組
- MouseWheel
- Moveable
- Name
- NavigationButtons
- NavigationCaption
- NewRecord
- OnActivate
- OnApplyFilter
- OnClick
- OnClose
- OnConnect
- OnCurrent
- OnDblClick
- OnDeactivate
- OnDelete
- OnDirty
- OnDisconnect
- OnError
- OnFilter
- OnGotFocus
- OnInsert
- OnKeyDown
- OnKeyPress
- OnKeyUp
- OnLoad
- OnLostFocus
- OnMouseDown
- OnMouseMove
- OnMouseUp
- OnOpen
- OnResize
- OnTimer
- OnUndo
- OnUnload
- OpenArgs
- OrderBy
- OrderByOn
- OrderByOnLoad
- Orientation
- Page
- Pages
- Painting
- PaintPalette
- PaletteSource
- Parent
- Picture
- PictureAlignment
- PictureData
- PicturePalette
- PictureSizeMode
- PictureTiling
- PictureType
- PivotTable
- PivotTableChange
- PopUp
- Printer
- Properties
- PrtDevMode
- PrtDevNames
- PrtMip
- Query
- RecordLocks
- RecordSelectors
- Recordset
- RecordsetClone
- RecordsetType
- RecordSource
- RecordSourceQualifier
- ResyncCommand
- RibbonName
- RowHeight
- ScrollBars
- Section
- SelectionChange
- SelHeight
- SelLeft
- SelTop
- SelWidth
- ServerFilter
- ServerFilterByForm
- ShortcutMenu
- ShortcutMenuBar
- SplitFormDatasheet
- SplitFormOrientation
- SplitFormPrinting
- SplitFormSize
- SplitFormSplitterBar
- SplitFormSplitterBarSave
- SubdatasheetExpanded
- SubdatasheetHeight
- Tag
- TimerInterval
- Toolbar
- UniqueTable
- UseDefaultPrinter
- VerticalDatasheetGridlineStyle
- ViewChange
- ViewsAllowed
- Visible
- Width
- WindowHeight
- WindowLeft
- WindowTop
- WindowWidth
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。