• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Key stroke event to trigger action on ActiveForm Text Field

I've got a search string input field, an ActiveX text box,  at the top of a worksheet.   The search works fine.   Right now I trigger the search by a double click on the text box.  I'm looking for a way to trigger the search with a key stroke, like a tab key or something, so that when I finish typing the search parameters, I don't have to reach for the mouse.

I tried adding a second, blank field which I can use to click on, to trigger "Lost Focus" on the search field, but that's still a mouse move.

I tried using the change event on the search field, with a control value (@) to indicated "I'm done entering search parameters", but it seems to loop all over itself when I try to remove the @.

I basically gave up on the ActiveX and built a little modal form for this purpose, however, I'd still like to know if / how it could be done with ActiveX on the worksheet.

Any help with this would be appreciated.
2 Solutions
If I understand you correctly, you have an ActiveX Text box placed on a worksheet.  You want to type into the textbox and when you press a key (Like the Enter key) you want it to go ahead and execute some code which, in this case, performs a search.  Is that accurate?

If that is accurate then I will give you a code snippet that should work for you.  Assuming that the name o the Textbox is: txtSearchBox, place this code in the KeyDown Event of the textbox.  Replace the MsgBox statement with the code that performs the search.

This code, checks each time you press a key.  When the key pressed is the Return key (13) this code will execute. As a beneift, It also checks to be sure that there is something in the textbox.

If KeyCode = 13 And Len(Me.txtSearchBox) > 0 Then

    MsgBox "Perform Search Here"

End If

Bob Oxford
Martin LissRetired ProgrammerCommented:
One slight modification in case someone enters or pastes in spaces.

If KeyCode = 13 And Len(Trim(Me.txtSearchBox)) > 0 Then
codequestAuthor Commented:
Works slick.  Thanks!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now