How to apply filter in a query?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eneyardi
    New Member
    • Jul 2010
    • 180

    How to apply filter in a query?

    I have a query name ICISquery, table field names are ARE of Employee, Date of ARE, Item Description and Remarks.
    The table field ARE of Employee is a name of employee.
    ICISquery has a 10000 records. I have a form name (home) in this form has a combolist and its row source are the name of employees. I have another form name (list) in datasheet view. when i click one of the employee in combolist, the list form will show up and its recordsource is ICISquery. But i want ICISquery will show only the records of employee i have clicked. For example, i click Juan Reynulfo, the list form will show up and contains only the records of Juan Reynulfo. Is this the work of filter in a query? If so, how can i apply this filter in query when you call it?
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    Hi again, eneyardi.
    Fortunately for you I know what you wish from this thread:
    How to shorten codes that using if, else and end if?
    Because from actual thread I can't understand :).

    So, one way to do this is to make a new module (or use an existing one.
    In this module delare a PUBLIC variable. Say strName and create a PUBLIC function (say fGetName)
    Something like this:
    Code:
    Option Explicit
    
    Public strName As String
    
    Public Function fGetName() As String
        fGetName = strName
    End Function

    In your query, in the field ARE criteria row, write: fGetName().

    Close the query and the module (of course save the changes).

    Now, go to your combo box (in design view) and, under On Click event write this code:
    Code:
    Private Sub ComboName_Click()
        strName = ComboName
    End Sub

    That must be all to do.
    From now to ever, when you select a new name from your combobox this name will be stored in strName variable. So, when you run the query (or something else like a form or a report based on this query) the query itself will apply (from the criteria row) the function fGetName which will return the value stored in strName (the name you preview selected in your combo box).

    Hope you understand the technique and how it work even my English is not very good.
    Last edited by NeoPa; Dec 28 '11, 04:32 AM. Reason: Fixed for display in Best Answer

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      That is certainly one way of doing, and under some circumstances can be very usefull.

      I will just for good measure mention another way of doing it:

      Presume you have your report set up intially to show all records of all employees. You can also choose to open the report and apply the filter at the same time.

      This could be done like so, presuming you have a simple combobox (comboSelectNam e) listing just the name:
      Code:
      Dim strName as string
      strName=Me.ComboSelectName
      
      Dim strFilter as string
      strFilter="EmployeeName='" & strName & "'"
      
      Docmd.OpenReport "NameOfReport",acViewPreview,,strFilter
      Now this will open the report in preview mode, with the filter applied. Its a very usefull feature. The same feature can also be used for forms btw.

      Comment

      • eneyardi
        New Member
        • Jul 2010
        • 180

        #4
        That's exactly what i'm looking for. thank you guys! I'm excited to apply that on my simple program, I'll give you the update later.

        Comment

        • eneyardi
          New Member
          • Jul 2010
          • 180

          #5
          Code:
          Dim strName As String
           strName = Me.Combo238
           
           Dim strFilter As String
          strFilter = "EmployeeName='" & strName & "'"
          
          DoCmd.OpenForm "list", acNormal, strFilter
          Smiley It shows all the records strfilter not working.
          The Recordsource of form name (list) is QueryofMasterli st the field EmployeeName = Combo238 rowsource
          Last edited by NeoPa; Dec 28 '11, 03:36 AM. Reason: Added mandatory [CODE] tags for you

          Comment

          • eneyardi
            New Member
            • Jul 2010
            • 180

            #6
            Mihail i followed your instructions but is not working, i don't know what is my fault. The recordsource became blank

            Comment

            • Mihail
              Contributor
              • Apr 2011
              • 759

              #7
              Remove all information from your database, ZIP it and attache it to your next post.
              Before removing information be sure you make a copy of your database (for safe).

              I can think about some reasons that my code make an empty record source:
              First can be that you don't use PUBLIC variable and/or function. I don't know if you use Option Explicit statement in yours modules.
              The second reason can be that your Combo238 has the first column hidden.
              I assume that Combo238 is a bound control. If it is not then we are in war with the wind mills.

              I have no idea why Smiley's code give you ALL the records ?!?!
              Have you an explication, Smiley ? (you know: I try to learn a little bit SQL. Thank you !)

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32653

                #8
                Please review [CODE] Tags Must be Used. After over a hundred posts you shouldn't still be posting this nonsense so that other people have to go around after you tidying up.

                I will simply delete any of your posts I see in future if they contain untagged code.

                Comment

                • eneyardi
                  New Member
                  • Jul 2010
                  • 180

                  #9
                  I'm sorry neopa, I'll do that next time.

                  Comment

                  • eneyardi
                    New Member
                    • Jul 2010
                    • 180

                    #10
                    Mihail thanks alot it works now! I only forgot to save. hehehe.

                    Comment

                    • Mihail
                      Contributor
                      • Apr 2011
                      • 759

                      #11
                      So, one query and five lines of code.
                      Is now enough short for you ? :)

                      Glad to help you.

                      Comment

                      • eneyardi
                        New Member
                        • Jul 2010
                        • 180

                        #12
                        Yup, it's a big help for me. Because we're using the program in LAN base, interaction speed improves alot. Thank you!
                        Last edited by NeoPa; Dec 29 '11, 02:29 PM. Reason: Quote unnecessary

                        Comment

                        Working...