组合框选择错误 2115

error 2115 on combobox selection

我在表单上有一个组合框,在我 运行 我的 SQL 访问查询之后,我试图以编程方式 select 组合框中的一项数据库。 我使用以下代码遍历项目并设置 selected 项目:

'Make the appropriate location appear in the combobox
For i = 0 To cboLocations.ListCount - 1
  If Me.cboLocations.Column(0, i) = locindex Then
    Debug.Print "locindex: " & locindex & vbCrLf & " Me.cboLocations.Column(0, i):" & Me.cboLocations.Column(0, i)
    Me.cboLocations.SetFocus
    Me.cboLocations.ListIndex = i '<<< error 2115
    Exit For
  End If
Next i

如前所述,我不断收到错误 2115:为此字段设置为 BeforeUpdate 或 ValidationRule 属性 的宏或函数阻止 Access 保存字段中的数据。

错误消息中指示的此组合框的两个属性均未设置为任何值。所以我卡住了。请指教

您可能正在与 BeforeUpdate 事件发生冲突。

尝试使用 AfterUpdate。

programmatically select one of the items in the combobox

我一直这样做的方法是像这样为组合的 Value 分配一些东西...

Me.MyCombo.Value = "target text"

Value 来自组合的 select 行的 绑定列 。 (您可以在组合的 属性 sheet 的 Data 选项卡上找到 Bound Column。)相反,分配 "target text"Value select 匹配行。

在您的情况下,我认为您正在尝试 select 包含与 locindex 变量相同文本的组合行。如果那是真的,那么我想你所需要的就是这个......

Me.cboLocations.Value = locindex

就您执行此操作而言,更新前验证规则 对我来说似乎都不是正确的选择。我建议您在查询后立即从您使用的任何代码到 运行 您的 "SQL query on the database" 执行此操作。