问题
I am working on creating filtering options of myReport contained in subform container. I have used solution described in this question: Filtering Report
from HansUp. Using described code filter is succesfully created, stored and applied with OnClick event of apllyFilterButton.
Then I create disableFilterButton and in OnClick event I put code suggested in before mentioned thread. While testing, I get this error:
"Run-time error '-2147417848 (80010108)':
Method 'FilterOn' of object '_Report_myReport' failed"
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.
Code:
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.
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
回答1:
Tomáš,
Since all of the code in your example is in the myForm class, let me explain some Access coding basics.
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.
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
is the name of the form class. This line of code will throw errors iffilterOptionOne
gets renamed or removed.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
in this context constrainsfilterOptionOne
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
Likewise:
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
This may not solve all your problems, but this should put you on a much sounder footing for understanding how to code Access forms.
来源:https://stackoverflow.com/questions/50715503/run-time-error-2147417848-80010108-when-disabling-filteron-property-of-report