访问过滤器 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4645971/
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
Access Filter VBA
提问by user569709
I'm trying to use a filter in vba like this:
我正在尝试在 vba 中使用这样的过滤器:
Private Sub Form_Load()
Me.Filter = "[Alvo] = " & AlvoAtual
Me.FilterOn = True
Me.Requery
End Sub
Where AlvoAtual is global variable, but nothin happens. When I change the AlvoAtual for a specifc value nothin happens too. Like this:
其中 AlvoAtual 是全局变量,但什么也没发生。当我将 AlvoAtual 更改为特定值时,也不会发生任何事情。像这样:
Private Sub Form_Load()
Me.Filter = "[Alvo] = 'AAAA'"
Me.FilterOn = True
Me.Requery
End Sub
Someone knows the problem?
有人知道问题吗?
I am adopting this question (VBAWhatnow) in the hope of it getting answered rather than make a duplicate as I was advised.
我正在采用这个问题 (VBAWhatnow),希望它得到回答,而不是像我被告知的那样重复。
I am trying to do the same thing except with local variables.
除了局部变量之外,我正在尝试做同样的事情。
My filter works correctly when I manually define the values but when I introduce the variables the filter no longer works
当我手动定义值时,我的过滤器工作正常,但当我引入变量时,过滤器不再工作
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = "Filterby = FilterCrit"
Could anyone help me find a good solution?
谁能帮我找到一个好的解决方案?
回答by HansUp
You (VBAWhatnow) said "My filter works correctly when I manually define the values but when I introduce the variables the filter no longer works".
您(VBAWhatnow)说“当我手动定义值时,我的过滤器工作正常,但是当我引入变量时,过滤器不再起作用”。
Then in your filter assignment ...
然后在您的过滤器分配中...
.Filter = "Filterby = FilterCrit"
So I'm assuming FilterCritis the name of your local variable. If that is true, build the filter expression using the variable's value rather than its name.
所以我假设FilterCrit是你的局部变量的名称。如果这是真的,请使用变量的值而不是其名称构建过滤器表达式。
If Filterbyis a numeric field type ...
如果Filterby是数字字段类型...
.Filter = "Filterby = " & FilterCrit
If Filterbyis a text field type ...
如果Filterby是文本字段类型...
.Filter = "Filterby = """ & FilterCrit & """"
If neither of those variations is the answer for you, give us more information about Filterbyand FilterCrit.
如果这些变体都不适合您,请向我们提供有关Filterby和 的更多信息FilterCrit。
回答by Mark3308
Hi try changing the code to the following:
嗨,尝试将代码更改为以下内容:
Private Sub Form_Load()
If Len(AlvoAtual & "") > 0 Then
Me.Filter = "[Alvo] = '" & AlvoAtual & "'"
Me.FilterOn = True
'Me.Requery
MsgBox AlvoAtual
Else
Msgbox "No Value set for variable AlvoAtual"
End If
End Sub
Provided the variable name is spelt right and contains a string value then the filter should work. Also provided you have a column name called Alvo in the current form.
如果变量名称拼写正确并包含字符串值,则过滤器应该可以工作。还假设您在当前表单中有一个名为 Alvo 的列名。
By the way I can't see why you'd need to do a Requery in the forms load event.
顺便说一句,我不明白为什么您需要在表单加载事件中执行 Requery。
Hope this helps
希望这可以帮助
回答by logan
Its problem with handling your single and double quotes. Try like this & will work
处理单引号和双引号的问题。像这样尝试&会起作用
Private Sub Form_Load()
Me.Filter = "[Alvo] = '" & AlvoAtual & "'""" 'make sure to copy all these quotes correctly. "" is to print 1 double quote.
Me.FilterOn = True
Me.Requery
End Sub
回答by Patrick Honorez
Try replacing your 3 instructions by this:
尝试用以下方法替换您的 3 条指令:
DoCmd.ApplyFilter wherecondition:= "[Alvo] = " & AlvoAtual
Note that you could also set the condition manually, and set the FilterOnLoad option to True and save your form. No programming required in that case.
"The best macro is no macro..."
请注意,您也可以手动设置条件,并将 FilterOnLoad 选项设置为 True 并保存您的表单。在这种情况下不需要编程。
“最好的宏就是没有宏……”

