+ Reply to Thread
Results 1 to 13 of 13

Pressing Enter on Textbox moves focus to next Textbox

  1. #1
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Pressing Enter on Textbox moves focus to next Textbox

    I have a couple of textboxes on a form. When I enter a number in the first textbox and press enter the rest of the textboxes are filled with data to view the data. The focus then jumps to the next textbox. I want the focus to always stay in the first textbox not jump to the next textbox.

    How can I get the focus to stay in the first textbox when I press enter key?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Use
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Pressing Enter on Textbox moves focus to next Textbox

    At the end of code set focus to same textbox

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Pressing Enter on Textbox moves focus to next Textbox

    I tried that and it didn't work (but I will try again). I have a sub that is run from the AfterUpdate event (of the textbox where I want the focus to stay) that populates the rest of the textboxes.

    Where would I put the TextBox1.SetFocus?

    I tried the Exit event of the textbox where I want the focus to stay.
    I tried the AfterUpdate event of the textbox where I want the focus to stay.
    I tried it as the last statement of the sub that is called that populates the textboxes.
    I even tried it in the Enter event of the next textbox where the focus goes when I press enter.

    None of the above worked. The focus still went to the second textbox for the first three options I tried. For the fourth option the focus went to the third textbox.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Suggest you post a sample workbook.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Pressing Enter on Textbox moves focus to next Textbox

    If both the .MultiLine property of a textBox and the .EnterKeyBehavior are True, pressing Enter will add a linefeed to the text in the box.
    If either is False, Enter will move the focus to the next control in the tab order.

    If you want a text box to completely ignore the pressing of the enter key, you could add this to the user form's code module

    Please Login or Register  to view this content.
    Another approach would be to put your Fill-Other-Boxes code in the BeforeUpdate event and set Cancel to True

    Please Login or Register  to view this content.
    Last edited by mikerickson; 10-09-2016 at 01:12 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Pressing Enter on Textbox moves focus to next Textbox

    I don't want to disable the Enter key. This is used to fire the search e.g user type 1234 Enter and the code finds entry 1234 and displays the details.

    Your second suggestion of setting Cancel = True makes the textbox behave exactly as I want but then I'm stuck in the textbox. Can't exit the textbox if the user clicks the Cancel button when done viewing entries.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Hi,

    Depending upon which event you are using to populate the other textboxes, you might simply set the Tabstop property of the other controls to False.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Again, suggest you post a sample workbook so we can test potential solutions.

  10. #10
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Attached a sample workbook.

    It does not help to make the TabStop properties false for all the fields. It just sets the focus to the Cancel button then.

    Find_Journal function does not work (disabled the code) as it references data in a huge workbook but you will get the idea.

    I want the focus to stay in the TxtJournalNumber textbox when entering a journal number in the TxtJournalNumber box and pressing Enter. Not jump to the next textbox.
    Attached Files Attached Files

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Pressing Enter on Textbox moves focus to next Textbox

    You might use the KeyDown event rather than the BeforeUpdate

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Thanks xlnitwit. It now works exactly as I wanted. Thanks again.

    p.s. Wanted to add to your reputation but forum won't let me

  13. #13
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Pressing Enter on Textbox moves focus to next Textbox

    Quote Originally Posted by Bezzie View Post
    ...It just sets the focus to the Cancel button then.
    Then set the .TabStop of that control to False also.

    From what I can see from that code, you don't really need text boxes (other than the one). Unless you anticipate the user entering into the control, a Label control would serve you as well as the secondary text boxes.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Fill textbox from another textbox when enter and car return
    By Alendi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-11-2013, 03:16 PM
  2. [SOLVED] Textbox focus
    By tradinup2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-08-2013, 12:25 AM
  3. Replies: 1
    Last Post: 01-14-2013, 10:52 PM
  4. [SOLVED] Enter the name of the worksheet (tab) in Textbox and after pressing the button to go to Sh
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-10-2012, 10:15 PM
  5. [SOLVED] Textbox - pressing 'Enter' when adding text
    By AliiShariff in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-06-2012, 05:19 AM
  6. Cursor moves when pressing <Enter>
    By nsv in forum Excel General
    Replies: 2
    Last Post: 02-07-2011, 06:54 AM
  7. Pressing any key in any textbox clears a cell?
    By scadaman29325 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2008, 09:46 AM
  8. Replies: 1
    Last Post: 02-22-2005, 06:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1