【发布时间】:2019-09-12 17:30:11
【问题描述】:
我找不到特定问题的答案,但我对 VBA 也比较陌生,所以也许我只是没有寻找正确的术语。 我所拥有的是一个包含一堆组合框的表单。它们按行和列排列,如下所示:
cboThingOne1 cboThingTwo1 cboThingThree1
cboThingOne2 cboThingTwo2 cboThingThree2
... ... ...
cboThingOne15 cboThingTwo15 cboThingThree15
我已设置 cboThingOne 以显示 SQL 数据库中的一系列项目(例如 department1、department2、department3、...)。
cboThingTwo 和 cboThingThree 也设置为某个 Rowsource(包含 Apple、Bananas 和 Cherries 等内容)。
我想要发生的是,一旦我将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 列。
这仍然无法帮助我更新 cboThingTwo 和 cboThingThree 的行源。
希望您能提供帮助,如果您需要更多信息,我很乐意提供。
【问题讨论】: