英文:
Use me.filter with 2 conditions - numeric and text
问题
我已创建一个按钮,用于筛选记录,使其在某个特定数字以下并具有特定类别。我无法让它工作。
我使用的代码是:
Private Sub KIAResearch_Click()
Dim strsc As String
strsc = "Killed in Action"
Me.Filter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And Service Category=" & strsc
Me.FilterOn = True
End Sub
英文:
I have created a button to filter records to be below a certain number AND with a specific category. I can't get it to work.
The code I have used is:
Private Sub KIAResearch_Click()
Dim strsc As String
strsc = "Killed in Action"
Me.Filter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And Service Category=" & strsc
Me.FilterOn = True
End Sub
答案1
得分: 1
看这里:如何在VBA中调试动态SQL
避免在列名中使用空格 - 如果必须使用,请在SQL中使用方括号:[Service Category]
。
要安全地连接带有SQL的变量,请使用Gustav的CSql()
函数。它处理字符串(需要引号)和其他变量。
所以:
strFilter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And [Service Category]=" & CSql(strsc)
Debug.Print strFilter
Me.Filter = strFilter
英文:
Have a look here: How to debug dynamic SQL in VBA
Avoid column names with spaces - if you must use them, you need square brackets in SQL: [Service Category]
.
To safely concatenate variables with SQL, use Gustav's CSql()
function. It handles strings (which need quotes) and other variables.
So:
strFilter = "WikiDegrees BETWEEN " & 1 & " AND " & 8 & " And [Service Category]=" & CSql(strsc)
Debug.Print strFilter
Me.Filter = strFilter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论