若要使用此示例:
-
在窗体上创建一个名为 "颜色" 的组合框。
-
Set the combo box's LimitToList property to Yes.
-
若要填充组合框, 请将组合框的**行来源** 类型属性设置为值列表。
-
提供以分号分隔的值列表作为**行来源** 属性的设置。 例如,可以提供后面这些值作为该属性的设置:Red; Green; Blue。
-
将以下事件过程添加到窗体中。 切换到"窗体"视图并在组合框的文本部分输入新值。
截 图:
代 码:
Private Sub Colors_NotInList(NewData As String, _
Response As Integer)
Dim ctl As Control
' 返回指向组合框的Control对象.
Set ctl = Me!Colors
' 提示用户确认他们想要添加新值.
If MsgBox("Value is not in list. Add it?", _
vbOKCancel) = vbOK Then
'设置Response参数以指示该数据正在添加
Response = acDataErrAdded
' 将NewData参数中的字符串添加到行源.
ctl.RowSource = ctl.RowSource & ";" & NewData
Else
' 如果用户选择“取消”,则禁止显示错误消息并撤消更改.
Response = acDataErrContinue
ctl.Undo
End If
End Sub