我的任务是创建一个由带有multiselect的列表框的结果过滤的查询。我有一个表,名为contacts,其中有一个名为“Source”的字段。这是在我的列表视图中显示的字段。我按“来源”的价值进行排序。
从这里,我可以选择多个选项,并点击一个提交按钮。当我这样做时,我得到一个查询,但它似乎只是显示所有的值。
因此,我的问题是,如何创建按源选项卡排序的查询?我知道我需要一个onclick函数,但我对VB非常陌生。任何帮助都将不胜感激!
发布于 2015-10-22 18:18:22
您可以修改以下内容以使用您的控件名称。您没有提到在何处/如何创建查询,但您可以将此代码与您的代码合并.
Private Sub cmdBuildWhere_Click()
Dim varItem As Variant
Dim strWhere As String
If Me.lstHierarchy.ListIndex < 1 And Me.lstHierarchy.ItemsSelected.Count = 0 Then ' No items selected
MsgBox "You did not select any sources. ", vbOKOnly, "Select Sources"
Exit Sub
End If
strWhere = "WHERE "
For Each varItem In Me.lstHierarchy.ItemsSelected
strWhere = strWhere & "[Sources] = '" & Me.lstHierarchy.Column(0, varItem) & "' Or "
Next varItem
' remove final ' or ' and add Sort
strWhere = Trim(left(strWhere, Len(strWhere) - 4)) & " Order By Sources;"
Debug.Print strWhere
End Subhttps://stackoverflow.com/questions/33287290
复制相似问题