+ Reply to Thread
Results 1 to 5 of 5

TextBox SetFocus in Controls Loop

  1. #1
    Registered User
    Join Date
    11-09-2016
    Location
    London, England
    MS-Off Ver
    2010/2016
    Posts
    71

    TextBox SetFocus in Controls Loop

    Hello,

    I have a userform for text input - 3 text boxes. Each must meet a certain criteria to be passed as ok. All this is done and working.

    What I am struggling with though is set focus. What I want, is if the first text box is passed OK but the other 2 aren't, the first text box with the error should be given focus. If text box 2 is ok but 1 and 3 are wrong, 1 should be given focus. If 1 and 2 are ok and 3 is wrong, 3 should have focus. And so on.

    I am doing this in a controls loop, then passing the text box object to a procedure which does the error checking and set the captions of appropriate labels and changes the border colour of the text box.

    As the SetFocus is currently part of the loop, it always sets focus to the last box with the error, because it just carries on the loop.

    I can't quite work out how to stop it continuing to set focus once focus has already been set. I don't want to exit the loop once an error is encountered because I want all error fields to show on in one go when pressing OK.

    Any ideas? In VBA can you find the first control that matches type and has certain criteria (i.e. specific border colour)? Without another loop?

    If you can, I could do all the error checking and flagging first, then something like this:

    Control.Parent.Controls(first textbox with red border).SetFocus

    Thanks
    Last edited by ldoodle; 06-02-2018 at 01:01 PM.

  2. #2
    Registered User
    Join Date
    11-09-2016
    Location
    London, England
    MS-Off Ver
    2010/2016
    Posts
    71

    Re: TextBox SetFocus in Controls Loop

    Please Login or Register  to view this content.
    I've simplified this bit:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: TextBox SetFocus in Controls Loop

    Perhaps loop through them in reverse? That way the last one found with an error would be the earliest on the form from the user's point of view.

    BSB

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,419

    Re: TextBox SetFocus in Controls Loop

    Turn your routine into a function and store reference to first occurance.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    11-09-2016
    Location
    London, England
    MS-Off Ver
    2010/2016
    Posts
    71

    Re: TextBox SetFocus in Controls Loop

    Thanks both

+ 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] SetFocus ActiveX TextBox ??
    By Logit in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 04-18-2020, 02:59 PM
  2. [SOLVED] Unable to setfocus on my textbox
    By JPGraphX in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-18-2014, 09:54 AM
  3. VBA Setfocus to next textbox
    By pjbassdc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2013, 12:47 PM
  4. loop controls in userform except one textbox
    By gkisacik in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-28-2010, 04:30 AM
  5. TextBox.SetFocus not working
    By vesoljc in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-11-2007, 01:49 AM
  6. [SOLVED] Having problems with textbox setfocus
    By Terry K in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2006, 09:30 AM
  7. setfocus in textbox on multipage
    By Martin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-06-2006, 12:25 AM

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