ComboBox 物件 (Access)
此物件對應到下拉式方塊控制項。 下拉式方塊控制項結合了文字方塊和清單方塊的功能。 當您想要用選項從事先定義的清單來輸入值或選取值時,請使用下拉式方塊。
控制措施 | 工具 |
---|---|
直到您在表單檢視按一下下拉式方塊的箭頭,Microsoft Access 才會顯示清單。
如果在您選取下拉式方塊工具之前,您有開啟控制項精靈,您可以使用精靈來建立下拉式方塊。 若要開啟或關閉控制項精靈,請在工具箱中按一下 [控制項精靈] 工具。
LimitToList 屬性的設定值決定您是否可以輸入不在清單中的值。
清單可以是單欄或是多欄,且可以顯示該欄的標題或沒有標題。
下列範例顯示如何使用多個 下拉式方塊 控制項,以提供查詢的準則。
Private Sub cmdSearch_Click()
Dim db As 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 [PymtTypeID]=" & Me.cboPaymentTypes
vWhere = vWhere & " AND [RefundTypeID]=" & Me.cboRefundType
vWhere = vWhere & " AND [RefundCDMID]=" & Me.cboRefundCDM
vWhere = vWhere & " AND [RefundOptionID]=" & Me.cboRefundOption
vWhere = vWhere & " AND [RefundCodeID]=" & Me.cboRefundCode
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
下列範例會示範如何將下拉式方塊的 RowSource 屬性設定載入表單時。 表單顯示時, cboDept 下拉式方塊中顯示儲存在 tblDepartment 下拉式方塊的 [ 部門 ] 欄位中的項目。
Private Sub Form_Load()
Me.Caption = "Today is " & Format$(Date, "dddd mmm-d-yyyy")
Me.RecordSource = "tblDepartments"
DoCmd.Maximize
txtDept.ControlSource = "Department"
cmdClose.Caption = "&Close"
cboDept.RowSourceType = "Table/Query"
cboDept.RowSource = "SELECT Department FROM tblDepartments"
End Sub
下列範例示範如何建立在顯示另一個資料行時系結至某個資料行的下拉式方塊。 設定 ColumnCount 的屬性為 2,即表示 cboDept 下拉式方塊會顯示由 RowSource 屬性所指定資料來源的前兩欄。 設定 BoundColumn 的屬性為 1 ,即表示當您檢查下拉式方塊的值時,會將儲存於第一欄的值傳回。
ColumnWidths屬性會指定兩個數據行的寬度。 藉由將第一欄寬度設定為 0 英吋,第一欄便不會顯示在下拉式方塊。
Private Sub cboDept_Enter()
With cboDept
.RowSource = "SELECT * FROM tblDepartments ORDER BY Department"
.ColumnCount = 2
.BoundColumn = 1
.ColumnWidths = "0in.;1in."
End With
End Sub
下列範例會顯示如何將項目加入到繫結的下拉式方塊中。
Private Sub cboMainCategory_NotInList(NewData As String, Response As Integer)
On Error GoTo Error_Handler
Dim intAnswer As Integer
intAnswer = MsgBox("""" & NewData & """ is not an approved category. " & vbcrlf _
& "Do you want to add it now?", vbYesNo + vbQuestion, "Invalid Category")
Select Case intAnswer
Case vbYes
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tlkpCategoryNotInList (Category) " & _
"Select """ & NewData & """;"
DoCmd.SetWarnings True
Response = acDataErrAdded
Case vbNo
MsgBox "Please select an item from the list.", _
vbExclamation + vbOKOnly, "Invalid Entry"
Response = acDataErrContinue
End Select
Exit_Procedure:
DoCmd.SetWarnings True
Exit Sub
Error_Handler:
MsgBox Err.Number & ", " & Err.Description
Resume Exit_Procedure
Resume
End Sub
- AfterUpdate
- BeforeUpdate
- Change
- Click
- DblClick
- Dirty
- Enter
- Exit
- GotFocus
- KeyDown
- KeyPress
- KeyUp
- LostFocus
- MouseDown
- MouseMove
- MouseUp
- NotInList
- Undo
- AddColon
- AfterUpdate
- AllowAutoCorrect
- AllowValueListEdits
- Application
- AutoExpand
- AutoLabel
- BackColor
- BackShade
- BackStyle
- BackThemeColorIndex
- BackTint
- BeforeUpdate
- BorderColor
- BorderShade
- BorderStyle
- BorderThemeColorIndex
- BorderTint
- BorderWidth
- BottomMargin
- BottomPadding
- BoundColumn
- CanGrow
- CanShrink
- Column
- ColumnCount
- ColumnHeads
- ColumnHidden
- ColumnOrder
- ColumnWidth
- ColumnWidths
- Controls
- ControlSource
- ControlTipText
- ControlType
- DecimalPlaces
- DefaultValue
- DisplayAsHyperlink
- DisplayWhen
- Enabled
- EventProcPrefix
- FontBold
- FontItalic
- FontName
- FontSize
- FontUnderline
- FontWeight
- ForeColor
- ForeShade
- ForeThemeColorIndex
- ForeTint
- Format
- FormatConditions
- GridlineColor
- GridlineShade
- GridlineStyleBottom
- GridlineStyleLeft
- GridlineStyleRight
- GridlineStyleTop
- GridlineThemeColorIndex
- GridlineTint
- GridlineWidthBottom
- GridlineWidthLeft
- GridlineWidthRight
- GridlineWidthTop
- Height
- HelpContextId
- HideDuplicates
- HorizontalAnchor
- Hyperlink
- IMEHold
- IMEMode
- IMESentenceMode
- InheritValueList
- InputMask
- InSelection
- IsHyperlink
- IsVisible
- ItemData
- ItemsSelected
- KeyboardLanguage
- LabelAlign
- LabelX
- LabelY
- Layout
- LayoutID
- Left
- LeftMargin
- LeftPadding
- LimitToList
- ListCount
- ListIndex
- ListItemsEditForm
- ListRows
- ListWidth
- Locked
- 名稱
- NumeralShapes
- OldBorderStyle
- OldValue
- OnChange
- OnClick
- OnDblClick
- OnDirty
- OnEnter
- OnExit
- OnGotFocus
- OnKeyDown
- OnKeyPress
- OnKeyUp
- OnLostFocus
- OnMouseDown
- OnMouseMove
- OnMouseUp
- OnNotInList
- OnUndo
- Parent
- Properties
- ReadingOrder
- Recordset
- RightMargin
- RightPadding
- RowSource
- RowSourceType
- ScrollBarAlign
- Section
- Selected
- SelLength
- SelStart
- SelText
- SeparatorCharacters
- ShortcutMenuBar
- ShowOnlyRowSourceValues
- SmartTags
- SpecialEffect
- StatusBarText
- TabIndex
- TabStop
- Tag
- Text
- TextAlign
- ThemeFontIndex
- Top
- TopMargin
- TopPadding
- ValidationRule
- ValidationText
- Value
- VerticalAnchor
- Visible
- Width
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。