+ Reply to Thread
Results 1 to 18 of 18

Moving focus back to first textbox

  1. #1
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Moving focus back to first textbox

    Hi guys

    I have two textboxes
    1. txtpolno
    2. Textbox5
    I am writing the following code to double check if the person has inputted the right policy number. If yes then the cursor should move to the next control but if not then the above two textboxes shoule be blank and the focus should be at txtPolNo.But the code moves the focus to the next control always.

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Can anyone please answer my question?

    Thanks

  3. #3
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: Moving focus back to first textbox

    hi,
    not sure but ....just try this one!
    Please Login or Register  to view this content.
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  4. #4
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    But if its in the change event of a textbox then it will fire even we type just 1 character in the textbox.

    Any other ideas?

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Moving focus back to first textbox

    The issue you have is with timing. If you step through your code you will see that the focus does get set to your control, but as soon as the routine ends, the focus switches to whatever control was next in the tab order. (basically, the tabbing to the next control is suspended while your code runs, then continues as normal.)
    Remember what the dormouse said
    Feed your head

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Moving focus back to first textbox

    I could not duplicate your problem. You can post a sample workbook if you like.

  7. #7
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Hi

    Please see attached the workbook. I hope you will be able to help me now.

    Thanks
    Attached Files Attached Files

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Moving focus back to first textbox

    Why do you allow the user to enter 'invalid' input in the first place ?
    By disabling all other objects in the frame you can force the focus to remain in the same textbox.



  9. #9
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Hi snb

    Thanks for your reply. In the actual workbook there are lots of textboxes and drop down boxes so I can disable all of them to resolve this issue.

    Can you think of any other workaround of this problem?

    Thanks

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Moving focus back to first textbox

    Like I said: put these two in a separate frame.
    Disable the textbox that hasn't the focus.
    Enable it if the input is correct.

    But why asking the user to enter the same thing twice ??

  11. #11
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Hi snb

    My manager asked me to double input thing for policy number like we normally have for the password creation in various websites.

    I tried putting the two textboxes in a frame and it still doesn't work as required.

    Can you make changes in the attached workbook so that I can come to know exactly how it goes.

    Thanks

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Moving focus back to first textbox

    How will the policy number's correctness be checked ?

  13. #13
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Snb, If the policy numbers in both textboxes are same then that means its right otherwise a message should display that policy numbers in two textboxes don't match with each other.

    Thanks

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Moving focus back to first textbox

    I see.
    If it's a new number why not generating it by the code ?
    If it's existing why not retrieve it from the database, according to name ?

  15. #15
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    snb, I think i didn't make it clear in the above posts. My company receives Insurance policies letters from its customers and now the users who will be using the system will input the policy numbers in the system by reading it from the letters.

    So we can't generate it automatically through the code.

    I hope you now understand my problem.

    I just have a problem of moving cursor at the right textbox and apart from that everything works fine.

    Thanks

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Moving focus back to first textbox

    I would either:
    1. use the Exit event to simply highlight both textboxes in red if they don't match (you could also use a label with an error message) and then have the Save button's code verify the two before it allows a save; or
    2. Simply cancel the input into the second textbox rather than trying to set the focus back to the first one.

  17. #17
    Registered User
    Join Date
    08-09-2011
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Moving focus back to first textbox

    Hi Romper

    I am writing the following code but still the focus still goes to the next control.Please help me to figure it out.
    Please Login or Register  to view this content.

  18. #18
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Moving focus back to first textbox

    I can't replicate that behaviour in your sample workbook.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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