vba 在过滤器字符串中使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11078904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using variables in a filter string
提问by VBwhatnow
I have a subform that returns data from a table. However I want to further filter this data by using the filter property.
我有一个从表中返回数据的子表单。但是我想通过使用 filter 属性进一步过滤这些数据。
I have a group of radio buttons, combo boxes and a case statement which sets the variables I want to use to the correct values depending on the radio button selection
我有一组单选按钮、组合框和一个 case 语句,它根据单选按钮的选择将我想要使用的变量设置为正确的值
My code for filling the variables works perfectly but I cannot use vba to set the filter unless I manually type the string that I want.
我用于填充变量的代码运行良好,但除非我手动输入所需的字符串,否则我无法使用 vba 来设置过滤器。
I assume that my issue is that my filter string is syntactically incorrect but I am unsure of how. Probably something to do with text delimiters.
我认为我的问题是我的过滤器字符串在语法上不正确,但我不确定如何。可能与文本分隔符有关。
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = Filterby = FilterCrit
Forms![frmPendingActions]![qryPendingAction subform].Form.FilterOn = True
Assume for this question that Filterby=[Reporter] and FilterCrit= Fake Name
对于这个问题,假设 Filterby=[Reporter] 和 FilterCrit= Fake Name
回答by Mark3308
Yes I think the problem is to do with text delimiters. The code should look similar to the following:
是的,我认为问题与文本分隔符有关。代码应类似于以下内容:
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = "[Reporter] ='" & FilterCrit & "'"
The filter should be built exactly the same as a where clause without the word where. If the column you are filtering by has a text datatype then the criteria needs to be enclosed in single or double quotes. If the column is a date datatype then it needs to be enclosed in #. If the column is a number datatype then it does not need to be enclosed.
过滤器的构建方式应与不带 where 一词的 where 子句完全相同。如果您筛选的列具有文本数据类型,则条件需要用单引号或双引号括起来。如果该列是日期数据类型,则需要将其括在 # 中。如果列是数字数据类型,则不需要将其括起来。
If you do not always wish to filter by the Reporter column then you can build a string using if statements or select case statements and then apply that string to the form filter.
如果您不总是希望按 Reporter 列进行过滤,那么您可以使用 if 语句或 select case 语句构建一个字符串,然后将该字符串应用于表单过滤器。
For example:
例如:
If [somecondition] Then
strFilter="[Reporter]='" & FilterCrit & "'"
Else
strFilter="[ID]=" & 0
End If
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = strFilter
I hope this helps.
我希望这有帮助。