Form filtering problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    Form filtering problem

    I have a form that is viewed in split form. The form's Filter On Load property is set to No because I want it to show all records when opened. I have a button to filter the form based on the current CustomerID. My problem is that when I click on the button, nothing appears to happen. If I click on it a second time, then the filter works. Here is my code:
    Code:
    Private Sub btnCustomerFilter_Click()
    Dim strCustomerID As Long
    strCustomerID = Me.CustomerID
    
    Form.FilterOn = True
    Me.Form.Filter = "CustomerID=" & strCustomerID
    
    End Sub
    I'm wondering if I'm doing things in the wrong order causing the filter to be turned on at the wrong time, but I don't know for sure. I have a Clear Filter button that works fine by turning off the filter. If I clear the filters using this button, and then re-filter with the button listed above, then it works the first time. It only requires the filter button to be clicked twice right after the form loads. I'm sure the solution is simple (they all seem simple when I learn what it is), but I can't figure it out.
  • dsatino
    Contributor
    • May 2010
    • 393

    #2
    try adding me.requery to your code

    Comment

    • patjones
      Recognized Expert Contributor
      • Jun 2007
      • 931

      #3
      Can you try reversing the order of lines 5 and 6 in your code?

      Pat

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        Thanks Pat. I figured that it was something to do with the order of my commands.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          You obviously have a good answer there, but I frequently formulate my similar code as :

          Code:
          Me.Form.Filter = {Whatever - can even result in no filter}
          Me.Form.FilterOn = (Me.Form.Filter > "")

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            To improve my education, what do you gain by using your code vs. what I had? I'm sure that you have a reason for doing it that way and I want to have my program be as well designed as possible.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Line #2 works appropriately (without any conditional code either) whether the filter is being set or cleared. FilterOn needs to be set correctly in both cases. Does that clarify?

              Comment

              • Seth Schrock
                Recognized Expert Specialist
                • Dec 2010
                • 2965

                #8
                Yes it does. Thanks.

                Comment

                Working...