How to create a "reset filter" cmd button?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vanlanjl
    New Member
    • Feb 2009
    • 46

    How to create a "reset filter" cmd button?

    I have created a form that contain two combo boxes and two cmd buttons. The idea of the form is that the user will select a search parameter from each combo box then will click the first cmd button "cmdCreateRepor t". Upon doing so it open a report based of the search criteria.

    My problems seems to lay with the seconf cmd button :
    cmdRemoveFilter

    Code:
    Private Sub cmdRemoveFilter_Click()
        On Error Resume Next
    ' Switch the filter off
        Reports![tblContacts].FilterOn = False
    End Sub
    also here is te code i used for cmdCreateReport

    Code:
    strWhere = "1=1 "
    If Not IsNull(Me.cboModel) Then
      strWhere = strWhere & " AND [ModelName] =""" & Me.cboModel & """ "
    End If
    If Not IsNull(Me.cboLocationCode) Then
      strWhere = strWhere & " AND [LocationCode] =""" & Me.cboLocationCode & """ "
    End If
    
    DoCmd.OpenReport "tblContacts", acPreview, , strWhere
    End Sub
    So here is the issue:
    I open the form and select my parameters and click on cmdCreateReport and it opens the report correctly. I then click the cmdRemoveFilter button and the report then will show all search parameters possable. I assumed that if I were to then go back and change the search parameters in the form and click cmdCreateReport a second time it would then excute properaly but instead the report stays as it was. So how do I make it where I can keep changing the filter processand make the report to produce the search each time?
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    In cmdCreateReport , close the report before you open it.
    I'm not sure whether this creates an error when the report is not open. If it does, you can use:
    If CurrentProject. AllReports("Rep ort Name").IsLoaded Then

    Comment

    • ChipR
      Recognized Expert Top Contributor
      • Jul 2008
      • 1289

      #3
      Assuming you tried .FilterOn = True in cmdCreateReport .

      Comment

      • vanlanjl
        New Member
        • Feb 2009
        • 46

        #4
        I am not dure what youare reffering too
        Originally posted by ChipR
        .FilterOn = True in cmdCreateReport .
        Do you mean in the properties for cmdCreateReport or are youtalking about the actual event Procedure/code?

        (Please bear with me I am a neewbie when it comes to vba but am trying to learn)

        Thanks,

        Comment

        • vanlanjl
          New Member
          • Feb 2009
          • 46

          #5
          In referance to:

          Originally posted by ChipR
          In cmdCreateReport , close the report before you open it.
          I'm not sure whether this creates an error when the report is not open. If it does, you can use:
          If CurrentProject. AllReports("Rep ort Name").IsLoaded Then

          I cant close the report until I close the form first. This seems redundant to have to do this each time I want to change the search parameters. You follow me?

          Comment

          • ChipR
            Recognized Expert Top Contributor
            • Jul 2008
            • 1289

            #6
            Why would you need to close the form before you close the report?

            I haven't actually tried this but what you may want to do in your cmdCreateReport is check whether the report is open. If it's not, open it the same way you do already. If it is, set it's .Filter property, then set .FilterOn=True.

            Comment

            • vanlanjl
              New Member
              • Feb 2009
              • 46

              #7
              Lol Okay i think there was a misscommunicati on in there somewhere. Your right I dont want to close the report. I want to be able to hit the cmdRemoveFilter and have it reset the report and I think reset the form. I think that is where the problem is. The report is resetting but the form is not. I still am not sure what you are reffering to when you say " .Filter" property
              Ther is no Filter option in the property tab for the cmd button, there is for the properties of the entire form.

              Comment

              • ChipR
                Recognized Expert Top Contributor
                • Jul 2008
                • 1289

                #8
                Your original post said that the report did not update when you selected new parameters on the form and clicked the cmdCreateReport button a second time. Is this still the problem you are trying to fix?

                The .Filter property is a property of the report. You used the .FilterOn property of the report already.

                I don't know what you mean by resetting the form, other than clearing the selections in the combo boxes.

                Comment

                • vanlanjl
                  New Member
                  • Feb 2009
                  • 46

                  #9
                  Okay for your first question: yes that is still my original problem, when i click the cmdCreateReport a second time it does not refilter the new results into the already open report.

                  As far as the properties settings on the form itself:
                  Filter: True
                  Filter On Load: yes

                  Your third staement about resetting the the combo boxes, yea lol, that seems like what needs to be done, but im not sure.
                  Thanks for help your thus far!

                  Comment

                  • ChipR
                    Recognized Expert Top Contributor
                    • Jul 2008
                    • 1289

                    #10
                    To solve your problem, then, you need to change your cmdCreateReport so that it works. Like I said in post #6, try:

                    Code:
                    strWhere = "1=1 " 
                    If Not IsNull(Me.cboModel) Then 
                      strWhere = strWhere & " AND [ModelName] =""" & Me.cboModel & """ " 
                    End If 
                    If Not IsNull(Me.cboLocationCode) Then 
                      strWhere = strWhere & " AND [LocationCode] =""" & Me.cboLocationCode & """ " 
                    End If 
                    
                    If CurrentProject.AllReports("tblContacts").IsLoaded Then 
                      Reports![tblContacts].Filter = strWhere
                      Reports![tblContacts].FilterOn = True
                    Else
                      DoCmd.OpenReport "tblContacts", acPreview, , strWhere 
                    End If
                    
                    End Sub

                    Comment

                    • vanlanjl
                      New Member
                      • Feb 2009
                      • 46

                      #11
                      Sweet!! Works - Thanks ChipR!!

                      Comment

                      Working...