Results 1 to 9 of 9
  1. #1
    data808 is offline Noob
    Windows 8 Access 2007
    Join Date
    Aug 2012
    Posts
    727

    Press Enter in text box to click Filter Button

    Basically I have Split Form with an unbound text box that is associated with a filter button. User will type in a value then click the filter button and everything works fine. However, I been trying to get it to where the user can also press Enter on the keyboard instead of having to click the button or of course press enter twice so that the button next to the text box gets focus and then clicks by the second Enter press. I have been messing with this for a while and did get it work eventually but its very odd. I had to repeat the VBA code twice for it to work in the KeyDown Event:



    Private Sub txtFilter_KeyDown(KeyCode As Integer, Shift As Integer)


    If KeyCode = vbKeyReturn Then
    Me.txtFilter.SetFocus
    Me.Filter = "[Clerk] Like '*" & Me.txtFilter & "*'"
    Me.FilterOn = True
    Me.Requery
    Me.txtFilter.SetFocus
    Me.Filter = "[Clerk] Like '*" & Me.txtFilter & "*'"
    Me.FilterOn = True
    Me.Requery
    Me.txtFilter.SetFocus
    End If


    End Sub

    If I don't repeat the code twice, it doesn't do the filter when I press the Enter key. Also, the Me.txtFilter.SetFocus doesn't seem to work as well. The focus still ends up on the button but I want it to end on the text box because then the user can type again without have to use the mouse or Shift Tab to go back to the box the try another search.

    Any help would be appreciated into figuring out what I am doing wrong. Thanks.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,936
    Perhaps use the key up event or change event instead?

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    What is the keypreview property value for your form?
    I might have used an approach of setting the tab order and tab stop and give the button a hotkey (like &Caption). Then 1st enter press moves focus then alt+C activates button.
    I don't think you should have to requery after applying a filter - unless you expect that someone else may have changed the loaded record(s) in the meantime.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    data808 is offline Noob
    Windows 8 Access 2007
    Join Date
    Aug 2012
    Posts
    727
    Quote Originally Posted by CJ_London View Post
    Perhaps use the key up event or change event instead?
    Just tried it on the Key Up Event and nothing happens. What is the difference between Key Up and Key Down?

  5. #5
    data808 is offline Noob
    Windows 8 Access 2007
    Join Date
    Aug 2012
    Posts
    727
    Key Preview property for the form is set to "Yes". Should I try set it to "No"?

    I will see if I can get different results without the Requery line. I wouldn't want to set a hotkey to perform a button click. That just doesn't fit the flow of my database. I need it to be intuitive and extremely user friendly for some of the more technologically illiterate people I have in my office.

  6. #6
    data808 is offline Noob
    Windows 8 Access 2007
    Join Date
    Aug 2012
    Posts
    727
    I was able to simplify it to this:

    If KeyCode = vbKeyReturn Then
    Me.FilterOn = True
    Me.Filter = "[Clerk] Like '*" & Me.txtFilter & "*'"
    Me.txtFilter.Setfocus
    End If

    The only problem I am having now is the Me.txtFilter.Setfocus does nothing. I think I know why. It's probably because the code is carried out before the cursor goes on from the text box to the next control which is the command button that I have assigned as the next tab stop for the focus. I have tried Key Up, Key Press, On Enter, On Exit events, etc... with no luck. Can someone help me figure this out?

    I would like the text box to retain the focus so that the user can continue to type into the text box trying multiple queries continuously back to back until they find what they are looking for.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,558
    Perhaps in the On_Enter of the next control, test if FilterOn is True, and if so, then set focus to txtfilter there?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,936
    What is the difference between Key Up and Key Down?
    the keydown event fires when you press the key, the keyup when you release the key.

    Either way

    Me.Filter = "[Clerk] Like '*" & Me.txtFilter & "*'"

    the value of txtFilter does not change until the control is updated - usually by moving the focus to another control.

    Whilst the control has the focus you should be using the controls text property

    Me.Filter = "[Clerk] Like '*" & Me.txtFilter.text & "*'"



  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Quote Originally Posted by data808 View Post
    Key Preview property for the form is set to "Yes". Should I try set it to "No"?
    Easy enough to try? Yes means the form should handle key events, no means the controls do.
    I would like the text box to retain the focus so that the user can continue to type into the text box
    Then you should be using the Change event and the text property as noted? Maybe research FAYT (find as you type) if you're wanting the filter to be modified after each key press. Not sure what the real issue is here. You're posting that the text box won't receive (or hold, not sure which) th focus in one post and saying the filter doesn't work in another.

    Make sure you step through your code and check variables and properties are what you expect. Or copy/compact/zip and post db here. See How to attach files at the very top.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. When press enter key will log in
    By dododo in forum Forms
    Replies: 1
    Last Post: 07-17-2013, 10:58 PM
  2. Replies: 23
    Last Post: 04-18-2013, 09:31 PM
  3. Replies: 3
    Last Post: 11-25-2012, 06:09 PM
  4. Filter subform on button Click
    By atom in forum Forms
    Replies: 2
    Last Post: 04-18-2012, 09:43 AM
  5. Replies: 0
    Last Post: 11-22-2011, 09:10 PM

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