经过广泛的研究,我还没有找到问题的答案。我在“多值”组合框领域遇到了困难。我希望能够将这个多值组合框添加到我的搜索表单中。但我的代码似乎不起作用。我的研究告诉我,多值字段的数据存储在其他地方。这是我的代码,我希望我能够指导正确的方向。
例如,“颜色”字段为“多值”下拉列表:红色,蓝色,黄色 您可以选择多个选项,因此该字段与其他字段不同。此代码适用于我所有其他领域。请让我知道你在想什么。谢谢
If Nz(Me.Color) <> "" Then
strWhere = strWhere & " AND " & "tblFlower.Color = '" & Me.Color & "'"
End If
答案 0 :(得分:0)
如果我理解得很好,你就是在谈论一个列表框,而不是一个组合框。
您需要遍历所选项目并将其添加到where子句:
Dim ctl As ListBox
Dim varItem As Variant
Set ctl = Me.Color
If ctl.ItemsSelected.Count >= 1 Then
'write begining of strWhere clause
strWhere = strWhere & " AND tblFlower.Color IN("
'iterate throught selected items to add them to the list)
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
Next varItem
'remove last comma and close parentheses
strWhere = Left(strWhere, Len(strWhere) - 1)
strWhere = strWhere & ")"
End If
如果您选择了这两种颜色,这应该将“AND tblFlower.Color IN('red','yellow')”添加到您的strWhere字符串。
编辑:用于多选组合框
组合框的值存储在一个数组中。你需要遍历这个数组:
Dim strWhere As String
Dim i As Integer
'first control if values have been selected
'then loop through array
If IsArray(Me.Color.Value) Then
'write begining of strWhere clause
strWhere = strWhere & " AND tblFlower.Color IN("
For i = 0 To UBound(Me.Color.Value)
strWhere = strWhere & "'" & Me.Color.Value(i) & "',"
Next i
'remove last comma and close parentheses
strWhere = Left(strWhere, Len(strWhere) - 1) & ")"
End If