Start search with 'Enter' key (1 Viewer)

sumdumgai

Registered User.
Local time
Today, 06:34
Joined
Jul 19, 2007
Messages
453
Need some help, please. I have a name lookup form with several search argument boxes; e.g., address, state, zip, etc. I do not want to initiate the search program with a 'Search' button, or when the user enters an argument and then tabs to the next search box. I would like to allow the user to input as many search arguments as he/she wants on the form, and then hit the 'Enter' key to begin the search.
Thanks for any help.
 

Micron

AWF VIP
Local time
Today, 06:34
Joined
Oct 20, 2018
Messages
3,478
On form property sheet, set KeyPreview property to Yes, then the form should get any keypress event before any control keypress event, assuming you have any. Try KeyPress event for the form to launch your search, as in If KeyAscii = vbKeyReturn Then...
Hopefully you are coding so that if some controls don't have data, the search will work properly if it's allowed to run.
 

sumdumgai

Registered User.
Local time
Today, 06:34
Joined
Jul 19, 2007
Messages
453
This is what I found elsewhere:
Code:
Private Sub fName_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        Me.Refresh
        MsgBox 1
        runSearch
        KeyCode = 0
    End If
End Sub


It's what Uncle suggested and seems to work. Do I still need to set KeyPreview to yes?
 

AccessBlaster

Registered User.
Local time
Today, 03:34
Joined
May 22, 2010
Messages
5,911
Need some help, please. I have a name lookup form with several search argument boxes; e.g., address, state, zip, etc. I do not want to initiate the search program with a 'Search' button, or when the user enters an argument and then tabs to the next search box. I would like to allow the user to input as many search arguments as he/she wants on the form, and then hit the 'Enter' key to begin the search.
Thanks for any help.

Maybe try this, no code required.
 

Attachments

  • Capture.JPG
    Capture.JPG
    97.8 KB · Views: 187

Micron

AWF VIP
Local time
Today, 06:34
Joined
Oct 20, 2018
Messages
3,478
Default will only work if the button has the focus. Since user is entering data, I suspect the last control edited will have the focus. User would have to give or code programmatically set the focus after the last control is edited, which won't work either if the user can/will edit controls in any random order.

You don't need preview on form, unless as noted, any form controls also have key events. Even then, it would depend on which you want to have priority. If you set it and forget and put key events on a control later, the form key event will fire first, which may or not be what you want, such as if the key event was to close the form. That's not something I'd ever do, but am just throwing it out there for an example.
 
Last edited:

sumdumgai

Registered User.
Local time
Today, 06:34
Joined
Jul 19, 2007
Messages
453
Zip code field is last search box on form. I used this:
Code:
Private Sub Zip_LostFocus()
    runSearch
End Sub


so if the user enters 'Zip' box and tabs out, whether entering data or not, the search starts.

Everything seems to work.


Thanks to all.
 

AccessBlaster

Registered User.
Local time
Today, 03:34
Joined
May 22, 2010
Messages
5,911
Default will only work if the button has the focus.
Huh, mine works fine without setting any focus. The point of the default action set to "yes" on any button is to tell access you want that button to simulate the enter key.

I could be wrong.:D
 
Last edited:

Micron

AWF VIP
Local time
Today, 06:34
Joined
Oct 20, 2018
Messages
3,478
Huh, mine works fine without setting any focus. The point of the default action set to "yes" on any button is to tell access you want that button to simulate the enter key.
I could be wrong.:D
Doesn't seem so. I was going by some hap-hazzard test I guess. Repeating it on another form is giving different results. While the form events do run first if key preview is Yes, only if the focus is on some other button does your suggestion not work. Thanks for the lesson!
 

AccessBlaster

Registered User.
Local time
Today, 03:34
Joined
May 22, 2010
Messages
5,911
Doesn't seem so. I was going by some hap-hazzard test I guess. Repeating it on another form is giving different results. While the form events do run first if key preview is Yes, only if the focus is on some other button does your suggestion not work. Thanks for the lesson!
Ah, after rereading your answer I see where we differ. I am only speaking of the button behavior (properties). I see now you are speaking of the form properties. Sorry bout that.

I have never used the key preview event, in fact I had to look it up.

Thanks
 

Users who are viewing this thread

Top Bottom