ComboBox.NotInList 事件 (Access)ComboBox.NotInList event (Access)

NotInList 事件發生於使用者在下拉式方塊清單中的下拉式方塊的文字方塊部分輸入值時。The NotInList event occurs when the user enters a value in the text box portion of a combo box that isn't in the combo box list.

語法Syntax

運算式NotInList(NewData回應)expression.NotInList (NewData, Response)

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

參數Parameters

名稱Name 必要/選用Required/Optional 資料類型Data type 描述Description
NewDataNewData 必要Required StringString Microsoft Access 會使用傳遞使用者在下拉式方塊的文字方塊部分中輸入事件程序的文字字串。A string that Microsoft Access uses to pass the text that the user entered in the text box portion of the combo box to the event procedure.
ResponseResponse 必要Required IntegerInteger 設定值表示 NotInList 事件處理的方式。The setting indicates how the NotInList event was handled. _Response_引數可以是下列其中一個內建常數:The Response argument can be one of the following intrinsic constants:
  • acDataErrDisplay (預設值)向使用者顯示預設的訊息。acDataErrDisplay (Default) Displays the default message to the user. 您可以使用這時您不想讓使用者新增至下拉式方塊清單的新值。You can use this when you don't want to allow the user to add a new value to the combo box list.
  • acDataErrContinue 不會向使用者顯示預設的訊息。acDataErrContinue Doesn't display the default message to the user. 您可以使用這當您想要向使用者顯示的自訂訊息。You can use this when you want to display a custom message to the user. 例如,事件程序無法顯示自訂對話方塊詢問使用者是否想儲存新的項目。For example, the event procedure could display a custom dialog box asking if the user wanted to save the new entry. 回應為是,如果事件程序會將新的項目新增至清單與 _Response_引數設為 acDataErrAddedIf the response is Yes, the event procedure would add the new entry to the list and set the Response argument to acDataErrAdded. 如果沒有回應,事件程序會將 acDataErrContinueResponse引數。If the response is No, the event procedure would set the Response argument to acDataErrContinue.
  • acDataErrAdded不會向使用者顯示一則訊息,但可讓您將項目新增至下拉式方塊清單中的NotInList事件程序。acDataErrAdded Doesn't display a message to the user but enables you to add the entry to the combo box list in the NotInList event procedure. 新增項目之後,Access 會更新由重新查詢下拉式方塊的清單。After the entry is added, Access updates the list by re-querying the combo box. Access 然後前景中重新檢查下拉式方塊清單中,針對字串並將值儲存在下拉式方塊結合的欄位中的_NewData_引數。Access then rechecks the string against the combo box list, and saves the value in the NewData argument in the field that the combo box is bound to. 如果字串不在清單中,Access 會顯示一則錯誤訊息。If the string is not in the list, Access displays an error message.

註解Remarks

發生此事件時若要執行巨集或事件程序,請將 OnNotInList 屬性設為巨集的名稱或 [事件程序]。To run a macro or event procedure when this event occurs, set the OnNotInList property to the name of the macro or to [Event Procedure].

此事件讓使用者可將新值新增至下拉式方塊清單中。This event enables the user to add a new value to the combo box list.

LimitToList 屬性必須是設定為 [是] 發生 NotInList 事件。The LimitToList property must be set to Yes for the NotInList event to occur.

NotInList 事件不會觸發 Error 事件。The NotInList event doesn't trigger the Error event.

NotInList 事件發生的下拉式方塊的 LimitToList 屬性設定為 [是]、 輸入不在清單中的值並嘗試移至另一個控制項或儲存記錄之後。The NotInList event occurs for combo boxes whose LimitToList property is set to Yes, after you enter a value that isn't in the list and attempt to move to another control or save the record. 事件發生於完成所有的下拉式方塊的 Change 事件。The event occurs after all the Change events for the combo box.

當**AutoExpand** 屬性設定為 [是] 時,Access 會在每次當使用者在下拉式方塊的文字方塊部分輸入字元,清單選取相符的值。When the AutoExpand property is set to Yes, Access selects matching values in the list as the user enters characters in the text box portion of the combo box. 如果在使用者輸入符合清單中值的第一個字元的字元 (例如,使用者輸入"Smith,"而 「 Smithson 」 是清單中的值),當使用者移動到另一個控制項或儲存的記錄時,不會發生NotInList事件。If the characters that the user types match the first characters of a value in the list (for example, the user types "Smith," and "Smithson" is a value in the list), the NotInList event will not occur when the user moves to another control or saves the record. 不過,字元,Access 會新增的字元,在使用者輸入 (在範例中,"兒子") 的下拉式方塊的文字方塊部分中選取。However, the characters that Access adds to the characters that the user types (in the example, "son") are selected in the text box portion of the combo box. 如果使用者想NotInList事件將觸發這種情況下 (例如,使用者想要將新的名稱"Smith"新增至下拉式方塊清單) 中,使用者可以輸入空格、 退格鍵或刪除字元之後的新值的最後一個字元。If the user wants the NotInList event to fire in such cases (for example, the user wants to add the new name "Smith" to the combo box list), the user can enter a Space, Backspace, or Delete character after the last character in the new value.

LimitToList屬性設定為 [是],並拉下下拉式方塊清單時,Access 會選取相符的值清單中使用者在下拉式方塊的文字方塊部分中輸入字元即使AutoExpand屬性設定為 [否]。When the LimitToList property is set to Yes and the combo box list is dropped down, Access selects matching values in the list as the user enters characters in the text box portion of the combo box, even if the AutoExpand property is set to No. 如果使用者按下 enter或移動到另一個控制項或記錄,將選取的值會出現在下拉式方塊。If the user presses Enter or moves to another control or record, the selected value appears in the combo box. 在此例中不會引發 NotInList 事件。In this case, the NotInList event will not fire. 若要允許 NotInList 事件將觸發,不應該下拉式下拉式方塊清單的使用者。To allow the NotInList event to fire, the user should not drop down the combo box list.

範例Example

下列範例會使用 NotInList 事件將項目新增至下拉式方塊。The following example uses the NotInList event to add an item to a combo box.

若要嘗試此範例中,建立名為Colors表單上的下拉式方塊。To try this example, create a combo box called Colors on a form. 將下拉式方塊的 LimitToList 屬性設定為 [是]。Set the combo box's LimitToList property to Yes. 若要填入此下拉式方塊,將下拉式方塊的 RowSourceType 屬性設定為 [值] 清單中,並提供的 RowSource 屬性設定為以分號分隔的值清單。To populate the combo box, set the combo box's RowSourceType property to Value List, and supply a list of values separated by semicolons as the setting for the RowSource property. 例如,您可能會提供下列值為此屬性的設定: 紅色;綠色;藍色。For example, you might supply the following values as the setting for this property: Red;Green;Blue.

接著,在表單上新增下列事件程序。Next, add the following event procedure to the form. 切換到 [表單檢視] 並在下拉式方塊的文字部分輸入新值。Switch to Form view and enter a new value in the text portion of the combo box.

注意

此範例會新增項目到未連結的下拉式方塊。This example adds an item to an unbound combo box. 當您要新增項目到連結的下拉式方塊,要將值新增到基準資料來源中的欄位。When you add an item to a bound combo box, you add a value to a field in the underlying data source. 在大多數情況下只是無法新增新的記錄; 中的一個欄位根據表格中資料的結構,您可能會需要新增一或多個欄位,以滿足資料需求。In most cases you can't simply add one field in a new record; depending on the structure of data in the table, you probably will need to add one or more fields to fulfill data requirements. 例如,新的記錄必須包含任何可包含主索引鍵的欄位的值。For example, a new record must include values for any fields comprising the primary key. 如果您需要以動態方式將項目新增至繫結的下拉式方塊中,您必須提示使用者輸入所需的所有欄位的資料、 儲存新的記錄,並再重新查詢下拉式方塊以顯示新的值。If you need to add items to a bound combo box dynamically, you must prompt the user to enter data for all required fields, save the new record, and then re-query the combo box to display the new value.

Private Sub Colors_NotInList(NewData As String, _ 
        Response As Integer) 
    Dim ctl As Control 
     
    ' Return Control object that points to combo box. 
    Set ctl = Me!Colors 
    ' Prompt user to verify they wish to add new value. 
    If MsgBox("Value is not in list. Add it?", _ 
         vbOKCancel) = vbOK Then 
        ' Set Response argument to indicate that data 
        ' is being added. 
        Response = acDataErrAdded 
        ' Add string in NewData argument to row source. 
        ctl.RowSource = ctl.RowSource & ";" & NewData 
    Else 
    ' If user chooses Cancel, suppress error message 
    ' and undo changes. 
        Response = acDataErrContinue 
        ctl.Undo 
    End If 
End Sub

下列範例會示範如何使用 NotInList 事件將項目新增至結合的下拉式方塊。The following example shows how to use the NotInList event to add an item to a bound combo box.

Private Sub cboDept_NotInList(NewData As String, Response As Integer)
    Dim oRS As DAO.Recordset, i As Integer, sMsg As String
    Dim oRSClone As DAO.Recordset

    Response = acDataErrContinue

    If MsgBox("Add dept?", vbYesNo) = vbYes Then
        Set oRS = CurrentDb.OpenRecordset("tblDepartments", dbOpenDynaset)
        oRS.AddNew
        oRS.Fields(1) = NewData
        For i = 2 To oRS.Fields.Count - 1
            sMsg = "What do you want for " & oRS(i).Name
            oRS(i).Value = InputBox(sMsg, , oRS(i).DefaultValue)
        Next i
        oRS.Update
        cboDept = Null
        cboDept.Requery
        DoCmd.OpenTable "tblDepartments", acViewNormal, acReadOnly
        DoCmd.GoToRecord acDataTable, "tblDepartments", acLast
    End If
End Sub

下列範例會顯示如何將項目加入到繫結的下拉式方塊中。The following example shows how to add an item to a bound combo box.

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 & ", " & Error Description
        Resume Exit_Procedure
        Resume

End Sub

支援和意見反應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.