如果在MS Access中查询,则组合框

问题描述

我创建了一个字段名'tool_used',其中指定了一些值,而'others'是其值之一。我们可以从此框中选择多个值。我想指定一个条件,如果该字段的值是其他值,则下一个可见的字段是“ other_tool”。我为此编写了一个代码,但是它给出了错误13,并且在我调试时显示请在字段中指定一个值。 请提供解决方

我的代码

Private sub tool_used_AfterUpdate() 
    If me.tool_used.value = "others" then 
        Me.other_tools.visible = true 
    Else 
        Me.other_tools.visible = False 
    End If 
End sub

解决方法

由于列表是多选的,因此您需要遍历ItemsSelected

Dim item As Variant

With YourListBox
    For Each item In .ItemsSelected
        Debug.Print .ItemData(item)
    Next item
End With

如果选择了多个项目,其中一个是“其他”,会发生什么?

编辑:

如果选择了“其他”选项,则循环并设置标志。否则,other_tools控件将保持隐藏状态。

Dim item As Variant,flag As Boolean

With YourListBox
    For Each item In .ItemsSelected
         If .ItemData(item) = "Other" Then flag = True: Exit For
    Next item
End With
    
other_tools.Visible = flag