ComboBox.AddItem 方法 (Access)ComboBox.AddItem method (Access)

將新的項目新增至指定的下拉式方塊控制項所顯示的值清單。Adds a new item to the list of values displayed by the specified combo box control.

語法Syntax

運算式AddItem(項目索引)expression.AddItem (Item, Index)

_運算式_代表**ComboBox** 物件的變數。expression A variable that represents a ComboBox object.

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
ItemItem 必要Required StringString 新項目的顯示文字。The display text for the new item.
IndexIndex 選用Optional VariantVariant 項目在清單中的位置。The position of the item in the list. 如果省略此引數,項目會新增到清單的最後。If this argument is omitted, the item is added to the end of the list.

註解Remarks

指定控制項的RowSourceType屬性必須設定為 Value List。The RowSourceType property of the specified control must be set to Value List.

此方法只適用於表單上的清單方塊或下拉式方塊控制項。This method is only valid for list box or combo box controls on forms.

清單項目號碼從零開始。List item numbers start from zero. 如果 Item 引數的值與現有項目號碼不相符,就會發生錯誤。If the value of the Item argument doesn't correspond to an existing item number, an error occurs.

至於多欄清單方面,請使用分號來隔開每一欄的字串 (例如,三欄式清單使用 "1010;紅色;大")。For multiple-column lists, use semicolons to delimit the strings for each column (for example, "1010;red;large" for a three-column list). 如果 Item 引數包含的字串比控制項的欄數還少,會從最左欄開始新增項目。If the Item argument contains fewer strings than columns in the control, items will be added starting with the left-most column. 如果 Item 引數包含的字串比控制項內的欄數還多,會略過多出來的字串。If the Item argument contains more strings than columns in the control, the extra strings are ignored.

使用 RemoveItem 方法來移除值清單的項目。Use the RemoveItem method to remove items from the list of values.

範例Example

此範例會將項目新增到清單方塊控制項的清單最後。This example adds an item to the end of the list in a list box control. 要讓函數生效,您必須傳送它一個 ListBox 物件 (代表表單上的清單方塊控制項),以及一個 String 值 (表示要新增項目的文字) 傳送給該函數。For the function to work, you must pass it a ListBox object representing a list box control on a form and a String value representing the text of the item to be added.

Function AddItemToEnd(ctrlListBox As ListBox, _ 
 ByVal strItem As String) 
 
 ctrlListBox.AddItem Item:=strItem 
 
End Function

此範例會將項目新增到下拉式方塊中的清單開頭。This example adds an item to the beginning of the list in a combo box control. 要讓函數生效,您必須傳送它一個 ComboBox 物件 (代表表單上的下拉式方塊控制項),以及傳送一個 String 值 (表示要新增項目的文字)。For the function to work, you must pass it a ComboBox object representing a combo box control on a form and a String value representing the text of the item to be added.

Function AddItemToBeginning(ctrlComboBox As ComboBox, _ 
 ByVal strItem As String) 
 
 ctrlComboBox.AddItem Item:=strItem, Index:=0 
 
End Function

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.