使用 me.filter,并设置两个条件 – 数值和文本。

huangapple go评论49阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月11日 17:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定