禁用报表的FilterOn属性时,运行时错误-2147417848(80010108) [英] Run time error -2147417848 (80010108) when disabling FilterOn property of Report
问题描述
我正在创建子窗体容器中包含的 myReport 的过滤选项. 我已使用此问题中描述的解决方案: 过滤报告
I am working on creating filtering options of myReport contained in subform container. I have used solution described in this question: Filtering Report
来自HansUp.使用所描述的代码过滤器可以成功创建,存储并与apllyFilterButton的OnClick事件一起应用.
from HansUp. Using described code filter is succesfully created, stored and applied with OnClick event of apllyFilterButton.
然后我创建disableFilterButton,并在OnClick事件中将前面提到的线程中建议的代码放入.在测试时,出现此错误:
Then I create disableFilterButton and in OnClick event I put code suggested in before mentioned thread. While testing, I get this error:
运行时错误'-2147417848(80010108)":
"Run-time error '-2147417848 (80010108)':
对象'_Report_myReport'的方法'FilterOn'失败"
Method 'FilterOn' of object '_Report_myReport' failed"
如果我使用Access Ribbon(打开/关闭开关过滤器按钮)启用/禁用过滤器,则一切正常,没有错误.因此,即使在语法上,我的错误也可能很简单,但是我找不到源.
If I enable/disable filter using Access Ribbon (switch-filter button on/off) everything works fine with no error. So my mistake might be simple, even in syntax, but I cant find the source.
代码:
Private Sub disableFilterButton_Click()
Forms![myForm]![myReport].Report.FilterOn = False
Forms![myForm]![myReport].Report.Filter = ""
Forms![myForm]![myReport].Requery
End Sub
崩溃后,使用调试"选项,上面的第一行代码将突出显示.我在网上的研究建议添加空白过滤器"行,以及注册表问题或代码中现有的循环,这会导致崩溃.但是,由于使用功能区按钮,一切正常,所以我认为它缺少一些简单的东西.我仍然是初学者.
After crash, using "Debug" option, the first row of code above is highlighted. My research here and around net suggested adding the "blank filter" row, and also registry problem or an existing loop in code, which creates the crash. But since using Ribbon buttons everything works, I think its something simple that I am missing. I am still an beginner.
myFilterButton中用于创建过滤器的代码尚未完全完成,但应该可以正常工作:
Code used in myFilterButton to create filter is not fully completed, but should be working:
Private Sub applyFilterButton_Click()
Dim ctl As Control
Dim varVyber As Variant
Dim filtrVolba As String
Dim filtrUplny As String
'Criteria creation for filter SQL
Set ctl = Forms![myForm]![filterOptionOne]
If ctl.ItemsSelected.Count <> 0 Then
For Each varVyber In ctl.ItemsSelected
filtrVolba = filtrVolba & ctl.Column(0, varVyber) & """ OR (sourceQuery.sourceColumn) = """
Next varVyber
filtrVolba = Left$(filtrVolba, Len(filtrVolba) - Len(" OR (qry_sourceQuery.sourceColumn) = "))
Forms![myForm]![myReport].Report.Filter = "(((sourceQuery.fieldBoundToMyReport)=[Forms]![myForm]![TextBoxBoundToMyReport]) AND ((sourceQuery.filterOptionOneSourceField) = """ & filtrVolba & "))"
Forms![myForm]![myReport].Report.FilterOn = True
Else
MsgBox "Not yet"
End If
End Sub
我们将不胜感激!托马斯
Help will be much appreciated! Thomas
推荐答案
Tomáš,
由于示例中的所有代码都在 myForm 类中,因此让我解释一些Access编码基础.
Since all of the code in your example is in the myForm class, let me explain some Access coding basics.
-
[Forms]![Form]![Control]
语法确实适用于Queries,Form&报告事件和属性和宏.虽然在VBA中有效,但没有 对此语法的编译时验证!因此,VBA行如下:
The
[Forms]![Form]![Control]
syntax is really intended for Queries, Form & Report events & properties, and Macros. While valid in VBA, there is no compile-time verification of this syntax! So, a VBA line like:
varValue = [Forms]![Blah]![BlahBlah] ' doesn't exist
将编译正常,但在运行时失败.
will compile OK, but fail at runtime.
带有类模块的表单中的控件成为表单类的公共成员.以您的形式 myForm :您可以使用以下代码在代码中引用其控件:
Controls in a form with a class module become public members of the form class. Take your form myForm: you can refer to its controls in code by using:
varValue = Form_myForm.filterOptionOne.Value
Form_myForm
是表单类的名称.如果filterOptionOne
被重命名或删除,则这行代码 将引发错误.
Form_myForm
is the name of the form class. This line of code will throw errors if filterOptionOne
gets renamed or removed.
在任何VBA类中,Me
均指该类的公共接口. 公共接口"仅表示类中定义的所有公共函数,子例程和属性.因此,如果您在 myForm 中使用上面的代码行,则最佳样式应如下所示:
In any VBA class, Me
refers to the public interface of that class. "Public interface" simply means all of the public functions, subroutines and properties defined in the class. So, if you were using the line of code above within myForm, the best style would look like this:
varValue = Me.filterOptionOne.Value
在这种情况下,
Me
约束了filterOptionOne
实际在类中定义,从而提供了进一步的编译时检查.
Me
in this context constrains filterOptionOne
to be actually defined in the class, providing further compile-time checks.
因此,鉴于以上三点,我将像这样重构您的代码:
So, in light of these three points, I would refactor your code like so:
Private Sub disableFilterButton_Click()
Me.myReport.Report.FilterOn = False
Me.myReport.Report.Filter = ""
Me.myReport.Requery
End Sub
类似:
Private Sub applyFilterButton_Click()
Dim ctl As Control
Dim varVyber As Variant
Dim filtrVolba As String
Dim filtrUplny As String
'Criteria creation for filter SQL
Set ctl = Me.filterOptionOne
If ctl.ItemsSelected.Count <> 0 Then
For Each varVyber In ctl.ItemsSelected
filtrVolba = filtrVolba & ctl.Column(0, varVyber) & """ OR (sourceQuery.sourceColumn) = """
Next varVyber
filtrVolba = Left$(filtrVolba, Len(filtrVolba) - Len(" OR (qry_sourceQuery.sourceColumn) = "))
Me.myReport.Report.Filter = "(((sourceQuery.fieldBoundToMyReport)=" & Me.TextBoxBoundToMyReport.Value & ") AND ((sourceQuery.filterOptionOneSourceField) = """ & filtrVolba & "))"
Me.myReport.Report.FilterOn = True
Else
MsgBox "Not yet"
End If
End Sub
这可能无法解决您的所有问题,但是这应该使您在了解如何编写Access表单代码方面拥有更坚实的基础.
This may not solve all your problems, but this should put you on a much sounder footing for understanding how to code Access forms.
这篇关于禁用报表的FilterOn属性时,运行时错误-2147417848(80010108)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!