Key stroke event to trigger action on ActiveForm Text Field

Posted on 2012-08-28
Last Modified: 2012-08-28
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.
Question by:codequest
    LVL 2

    Accepted Solution

    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
    LVL 44

    Assisted Solution

    by:Martin Liss
    One slight modification in case someone enters or pastes in spaces.

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

    Author Closing Comment

    Works slick.  Thanks!

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
    This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
    This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now