【发布时间】:2017-09-15 04:57:24
【问题描述】:
我创建了一个 Access 表单并实现了 3 个“级联组合框”,所以本质上,如果用户在组合框 1 中选择一个项目,这将过滤组合框 2 中的项目,然后过滤组合框中的项目3.
我在 VBA 中使用此代码:
Private Sub ComboBox1_AfterUpdate()
Me.Combobox2.RowSource = "SELECT DISTINCT [Table1].[SubColumn1]" & _
"FROM [Table1] " & _
"WHERE [Column1] = '" & Combobox1.Value & "' ;"
End Sub
这很好用。但是,在某些情况下,combobox2 中没有可供选择的项目。我在“Table1”中的数据如下所示:
| Column 1 | SubColumn1 | SubColumn2 |
|------------- |------------- |------------- |
| Item 1 | Item 1A | |
| Item 1 | Item 1B | |
| Item 2 | | |
| Item 3 | Item 3A | Item 3A1 |
| Item 3 | Item 3B | Item 3B1 |
所以如你所见,如果用户从combobox1 中选择了Item 2,combobox2 或combobox3 中将没有任何信息。
既然如此,我可能会修改此代码,如果在组合框 2 或 3 中没有可用的项目,这些组合框将被隐藏。
我尝试将整个表达式包装在 if 语句中,如下所示:
Private Sub ComboBox1_AfterUpdate()
if isnull(Me.Combobox2.RowSource = "SELECT DISTINCT [Table1].[SubColumn1]" & _
"FROM [Table1] " & _
"WHERE [Column1] = '" & Combobox1.Value & "' ;" )
then me.combobox2.visible = false
else: me.combobox2.visible = true
End Sub
这不起作用,combobox2 和 3 不仅没有消失,而且现在盒子中没有任何项目。
有人可以帮忙吗?
不确定这一切有多清楚,所以如果我能回答任何问题,请告诉我!
更新
这是我为 3 个组合框设置的两个宏:
Private Sub ComboCause1_AfterUpdate()
Me.ComboCause2.RowSource = "SELECT DISTINCT [Cause of Injury].[Cause of Injury 2]" & _
"FROM [Cause of Injury] " & _
"WHERE [Cause of Injury] = '" & ComboCause1.Value & "' ;"
If Me.ComboCause2.ListCount < 2 Then
Me.ComboCause2.Visible = False
Me.ComboCause3.Visible = False
Me.Label2695.Visible = False
Me.Label2699.Visible = False
Else:
Me.ComboCause2.Visible = True
Me.Label2695.Visible = True
End If
End Sub
和
Private Sub ComboCause2_AfterUpdate()
Me.ComboCause3.RowSource = "SELECT DISTINCT [Cause of Injury].[Cause of Injury 3]" & _
"FROM [Cause of Injury] " & _
"WHERE [Cause of Injury 2] = '" & ComboCause2.Value & "' ;"
If Me.ComboCause3.ListCount < 2 Then
Me.ComboCause3.Visible = False
Me.Label2699.Visible = False
Else:
Me.ComboCause3.Visible = True
Me.Label2699.Visible = True
End If
End Sub
【问题讨论】:
-
在
ComboCause1_AfterUpdate的 If 语句中,我认为您需要删除组合 3 测试。我真的不明白那里的逻辑。在ComboCause2_AfterUpdate的 If 语句中,您需要将And更改为Or。 -
开始工作了!如果您想看一下,我对原始编辑进行了一些更改。一切似乎都在工作!感谢大家的帮助!
标签: vba ms-access ms-access-2010