+ Reply to Thread
Results 1 to 18 of 18

Moving focus back to first textbox

Hybrid View

  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.

    Private Sub TextBox5_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If txtPolNo.Value <> TextBox5.Value Then
    MsgBox "The Policy numbers don't match with each other"
    TextBox5.Value = ""
    txtPolNo.Value = ""
    txtPolNo.SetFocus
    Cancel = True
    End If
    End Sub

  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,060

    Re: Moving focus back to first textbox

    hi,
    not sure but ....just try this one!
    Private Sub TextBox5_Change()
    If TextBox1.Value <> TextBox5.Value Then
    MsgBox "The Policy numbers don't match with each other"
    TextBox1.SetFocus
    TextBox1.Value = ""
    TextBox5.Value = ""
    End If
    TextBox1.SetFocus
    End Sub
    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 Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,982

    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.)
    Everyone who confuses correlation and causation ends up dead.

  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 romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,982

    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.

  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 Romper

    I am writing the following code but still the focus still goes to the next control.Please help me to figure it out.
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If txtPolNo.Value <> TextBox2.Value Then
    MsgBox "The Policy numbers don't match with each other"
    txtPolNo.BackColor = vbRed
    TextBox2.BackColor = vbRed
    TextBox2.Value = ""
    txtPolNo.Value = ""
    
    Cancel = True
    End If

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,982

    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