Results 1 to 11 of 11
  1. #1
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368

    Problem with filter

    Im trying to filter records on my form using the enter key.


    Like so :

    Code:
    Private Sub filterplaatsnaam_Enter()
    On Error GoTo Err_filterplaatsnaam_Enter
    
    
    FilterForm
    
    
    Exit_filterplaatsnaam_Enter:
        Exit Sub
    
    
    Err_filterplaatsnaam_Enter:
        MsgBox Err.Description
        Resume Exit_filterplaatsnaam_Enter
    
    
    End Sub
    Then the filter form sub :

    Code:
    Private Sub FilterForm()
    
    
        Dim strFilter As String
        Dim blnFilter As Boolean
        
        blnFilter = False
        
        strFilter = " 1=1 "
        If Me.filterplaatsnaam <> "" Then
            strFilter = strFilter + " and plaatsnaamobject like '" & filterplaatsnaam & "*'"
            blnFilter = True
        End If
        
        If Me.fldFilterObject <> "" Then
            strFilter = strFilter + " and NaamObject like '" & fldFilterObject & "*'"
            blnFilter = True
        End If
        
        If Me.kzlFlterObject <> "" Then
            strFilter = strFilter + " and SoortObject like '" & kzlFlterObject & "*'"
            blnFilter = True
        End If
        
        
        If blnFilter Then
            Me.Filter = strFilter
        Else
            Me.Filter = "1=2"
        End If
       
        Me.FilterOn = True
    
    
    End Sub
    Its just that when i move from one field i search in to the other, the filter is applyed.
    Also, the field kzlFlterObject is an drop down menu, and the filter doesn't work on that field.

    Any ideas ?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Looks good except
    strFilter = " 1=1 " and strFilter = " 1=2 "
    why?

    I dont see why these are needed. Remove them.
    To remove filter, just do filteron = false.
    And youre supposed to filter the FIELD, NOT the object name. so field: [SortObject] like


    (if still not working, it may be your objects. Im not sure what kind of data is in SortObject, NameObject.)

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    filterplaatsnaam_Enter
    it is because you are applying the filter when you enter a control, try putting the calling code in the afterupdate event instead

  4. #4
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    If i do that the code will run once i exit the field. And i want to be able to input multiple criterea before i hit the enter key.

    But i think your at the right track to help me because im beginning to think that _enter is not the enter keypress event..

    Is that the case ?

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    correct - the enter event is when you enter a control (either by tabbing into it or clicking on it) - it is very similar to the gotfocus event

    you will need a separate button to 'trigger' the filtering when clicked.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I would avoid using the "Enter" key to fire code. You could, possibly, employ SendKeys. However, this will affect the User's experience in an undesirable way. Use a button to fire the code. The User can always Tab to the button and then hit Enter.

  7. #7
    keviny04 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Apr 2015
    Posts
    128
    Quote Originally Posted by JeroenMioch View Post
    Its just that when i move from one field i search in to the other, the filter is applyed.
    If you don't want the filter to apply when you leave each field, then you need a command button for the user to apply it. The user has got to have a way to tell you that he wants the filter to run.

    Quote Originally Posted by JeroenMioch View Post
    Also, the field kzlFlterObject is an drop down menu, and the filter doesn't work on that field. Any ideas ?
    Check if kzlFlterObject actually returns the value you want, either "" or a valid string. Also, watch for single quotes in all the entry fields. If the user enters something like "April Fool's Day", strFilter will evaluate into: and SoortObject like 'April Fool's Day*' , which will produce an error. Replace any single quote with TWO single quotes:

    strFilter = strFilter + " and SoortObject like '" & Replace(kzlFlterObject, "'", "''") & "*'"

    Naturally, you can't allow the user to enter asterisks as well, since your filter expression uses it. Any special character used by the filter expression, you need to trap it from user input.
    Last edited by keviny04; 04-22-2015 at 09:00 AM.

  8. #8
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    I still believe that it can be done. We have another application running and it works the same way.
    Unfortuantely i cant get the code.

    Cant i use keypress 13 ?

  9. #9
    keviny04 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Apr 2015
    Posts
    128
    Quote Originally Posted by JeroenMioch View Post
    I still believe that it can be done. We have another application running and it works the same way.
    Unfortuantely i cant get the code.

    Cant i use keypress 13 ?
    If you want the user to use only keystrokes to do searches, you can still accomplish it with a command button. Assign a shortcut key to the button. In the button's caption, put an ampersand next to the alphabet that you want the shortcut to be. E.g. use "&Search" as the caption, and the button will show the shortcut alphabet underlined: Search. When the user presses Alt-s, the button is "clicked". This way you allow both mouse and keyboard actions and serve the most users.

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have said What you are trying to do, but not How you are trying to do it.

    You have a bound form with controls in the details section?
    There are unbound controls in the form header that you want to use to filter the dataset?

    If this is correct, you should look at the code here:
    How to build a criteria string form the non-blank search boxes.
    http://www.allenbrowne.com/ser-62code.html

    Place the code in the form module. You will have to modify the code for your unbound control names.
    In the afterupdate event of the unbound controls, you should call the sub "cmdFilter_Click".
    Every time an unbound control gets updated, the afterupdate code runs and changes the filter.

    There is also code to attach to a button to reset the filter and clear the unbound controls ("cmdReset_Click").

  11. #11
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I, too, have reservations about using <Enter> to do things other than its native function, as many experienced Access users use it to navigate through the Controls, instead of using the Tab Key, and having it not do that may irritate them...but that's your choice; maybe your end users aren't Access savvy!

    You will need a Command Button, as has been said, to trigger the filtering, but you can then run the code behind the Command Button by simply pressing the Enter Key!

    In Form Design View

    1. Select the Command Button
    2. Go to Properties - Other
    3. You will see a Property named Default; change this Property from 'No' to 'Yes'

    Now, when you hit <Enter> it will be the same as clicking on the Command Button! Obviously, this can only be done for one Command Button on any given Form!

    Linq ;0)>

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filter problem
    By cbende2 in forum Access
    Replies: 5
    Last Post: 03-11-2015, 02:11 PM
  2. DAO Filter Problem
    By tad888 in forum Access
    Replies: 3
    Last Post: 08-13-2014, 12:42 PM
  3. Filter after pre Filter problem
    By gg80 in forum Programming
    Replies: 6
    Last Post: 07-18-2013, 10:45 AM
  4. filter problem
    By masoud_sedighy in forum Forms
    Replies: 2
    Last Post: 12-15-2011, 01:03 AM
  5. Filter problem
    By Callahan in forum Forms
    Replies: 0
    Last Post: 07-06-2011, 08:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums