How to use the enter key as a command button.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sywalton
    New Member
    • Jun 2014
    • 3

    How to use the enter key as a command button.

    I have a search query that returns records based on keywords entered into a search field. In order to run the query that returns the records I have to push a command button. I would rather just hit the enter(return)ke y to perform the same function and being somewhat of a novice, am having difficulty figuring out how to do this.

    Dose anyone have code that I can use to make this happen?
    Thanks
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    #2
    sywalton,

    Make sure your Command Button has a Tab stop attached to it and adjust the Tab Order of the Form so that it comes immediately after the Search Field. Then, when you hit [Tab] or [Enter] from the Search Field, your command Button should have the focus and you can hit Enter again and your Command Button will execute its code.

    You could also change the Command Button's GotFocus Event to execute the code, but I try to avoid that, as the command button could get the focus for various reasons.

    Hope this hepps!

    Comment

    • jimatqsi
      Moderator Top Contributor
      • Oct 2006
      • 1288

      #3
      You might consider using the GotFocus and LostFocus events to change the color of the text on the button, or some other property, so it is more obvious where the focus is.

      Jim

      Comment

      • Hennepin
        New Member
        • Oct 2009
        • 25

        #4
        Change the Default property of command button to yes.
        Then when you hit enter the command button will execute even if it does not have focus or a tab stop.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3653

          #5
          Hennepin,

          Thanks for the advice! I forgot that this option was available on the command button object. There can be challenges with that in some cases, but I can see how it would work well in this situation.

          Of course, yet another way to do this is to establish AfterUpdate Events for all of your data fields. The code would initially check to make sure the fields were poulated and/or acceptable, and then automatically populate any other fields based on the user's input. If the required data was not supplied, then the code would do nothing, to prevent any errors on the import.

          As usual, a lot of ways to do the same thing. The DB manager should evaluate their options to see which will work best for any situation.

          Comment

          • sywalton
            New Member
            • Jun 2014
            • 3

            #6
            Will changing the default property effect other buttons I have on the form. Their are 10 other buttons on this particular form.

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3653

              #7
              sywalton,

              Only one command box on a form can have its default property set to "Yes". All others will automatically be set to no.

              With the extra command buttons, unless you want to use VBA to perform your updates automatically, using the tab and enter method may work best....

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                no

                Please keep in mind.

                If button [OK] has default set to true

                and any other button (say button [Cancel] ) has the focus

                then the button with the focus will be the toggled acction.

                If a listbox, textbox, or other non-command button control has the focus, and there is a default command button set, then pressing enter will toggle that command button.

                This can be irritating for the end user who may be expecting to use the enter key to move from one field to the next.

                Also be aware that the default command button will usually receive the focus on form open; thus, you will need to set the proper focus if this is not desired.

                Comment

                • sywalton
                  New Member
                  • Jun 2014
                  • 3

                  #9
                  This worked out perfectly. Thank you so much for the help.

                  Comment

                  Working...