【问题标题】:Access Checkbox value update does not trigger AfterUpdate event访问复选框值更新不会触发 AfterUpdate 事件
【发布时间】:2019-09-12 17:30:11
【问题描述】:

我找不到特定问题的答案,但我对 VBA 也比较陌生,所以也许我只是没有寻找正确的术语。 我所拥有的是一个包含一堆组合框的表单。它们按行和列排列,如下所示:

cboThingOne1   cboThingTwo1   cboThingThree1
cboThingOne2   cboThingTwo2   cboThingThree2
...            ...            ...
cboThingOne15  cboThingTwo15  cboThingThree15

我已设置 cboThingOne 以显示 SQL 数据库中的一系列项目(例如 department1department2department3、...)。

cboThingTwocboThingThree 也设置为某个 Rowsource(包含 AppleBananasCherries 等内容)。

我想要发生的是,一旦我将cboThingOne1 中的值更改为department3,以使用相同的值填充所有cboThingOne 复选框。 这是有效的。我正在使用每个 cbo 的 AfterUpdate 事件来调用一个函数,该函数遍历 Me.Controls 中的所有项目,检查它们的名称 + 1(在本例中为 cboThingOne2)并将此控件的值设置为 department3

Private Sub fun_fill_cbo_with_value(FieldName As String, FieldValue As String)
  Dim i as Integer
  'This function returns the name without number
  my_fieldname = Striptext(FieldName)
  'gets me the number of the field I am working with (i.e. cboThingOne1 --> 1)
  i = Int(Replace(FieldName, my_fieldname, ""))
  i = i+1
  cbo_name = my_fieldname + CStr(i)
  For Each my_control In Me.Controls
    If my_control.Name = my_fieldname Then
      my_control.Value = FieldValue
    End If
  Next my_control
End Sub

cboThingOne2的值发生变化时,我预计会触发这个cbo的AfterUpdate事件,这显然不会发生。

在此更新后事件中,cboThingTwo2 的行源应该被更新。

Private Sub cboThingOne2_AfterUpdate()
  'To update the cbo in the row below
  Call fun_fill_cbo_with_value(cboThingOne2.Name, cbo_ThingOne2.value)
  Me.cboThingTwo2.RowSource = "somedifferentqueryhere"
  Me.cboThingThree2.Rowsource = "yetanotherquery"

我的预期是,更新 fun_fill_cbo_with_value 中的值会更新 cbo + 1 的值(有效)并触发 AfterUpdate 事件(不会发生)。

此外,因为我在 AfterUpdate 事件中调用 fun_fill_cbo_with_value,我希望该列填充到最后,而不仅仅是下面的 cbo(就像现在一样)。

我可以通过在 fun_fill_cbo_with_value 中添加一个从 1 到 15 的附加循环来填充整个 cbos 列。

这仍然无法帮助我更新 cboThingTwocboThingThree 的行源。

希望您能提供帮助,如果您需要更多信息,我很乐意提供。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    我预计会触发此 cbo 的 AfterUpdate 事件,这 显然不会发生。

    它仅由用户操作触发。

    从您的代码中调用 AfterUpdate 事件函数本身(不好的做法);或(允许更好地组织代码)让 AfterUpdate 事件调用一个单独的函数,您的其他代码也可以调用该函数。

    【讨论】:

    • 我已经尝试过类似的方法,但在那里失败了。我试图调用当前 my_control 的 AfterUpdate 事件,但显然无法正确使用语法。我如何从fun_fill_cbo_with_value 调用它?另外,例如,我将如何构造 cboThingTwo2 名称?所以我可以在fun_fill_cbo_with_value 中添加一个执行cbo.ThingTwo2.Rowsource = "something" 的部分。尝试了curr_cbo = "Me." & cbo_name 之类的东西,然后尝试了curr_cbo.RowSource = "something",但没有奏效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多