+ Reply to Thread
Results 1 to 3 of 3

Using .SetFocus inside a TextBox_Exit event causes _Exit event to fire twice.

  1. #1
    Registered User
    Join Date
    09-30-2011
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    19

    Using .SetFocus inside a TextBox_Exit event causes _Exit event to fire twice.

    I have a userform I'm creating for work that will contain batch job information. I have 30 rows of textboxes, each row has a separate Job Order, Part Number, and Customer textbox. When the user enters a Job Order number in the first field, the JobOrder_Exit code fires, disabling the Part Number and Customer fields and then pulling in job information from our SQL server to autofill the Part Number and Customer textboxes. If there is no job order number in the system, then the job is a custom and the Part Number and Customer fields are enabled so the user can fill those with custom information.

    I want to give the form the capability of detecting duplicate entries (there ARE 30 rows after all!). If the user enters a job order number, I want the code to be able to detect if that number has already been entered on the form and then display a msgbox stating this was a duplicate number.

    Good so far! I've got that working, what DOESN'T work is that I want to then clear the textbox they just entered and then highlight the box that has the first instance of that duplicate job order number. I tried doing this with the .SetFocus method, but unfortunately using the .SetFocus necessarily removes the focus off the current textbox thereby re-firing the textbox_Exit code resulting in the duplicate textbox NOT being highlighted; the next textbox is selected as if there was no problem.

    How do I highlight a different textbox FROM textbox_exit change event?

    Here's the general code structure:
    Please Login or Register  to view this content.
    Last edited by Fattyfatfat Kid; 07-29-2015 at 07:22 PM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Using .SetFocus inside a TextBox_Exit event causes _Exit event to fire twice.

    Hi Fattyfatfat Kid,

    If I understand your problem correctly, it seems like you need the UserForm equivalent of Application.EnableEvents. Since Excel doesn't provide one, I usually use a module global boolean variable to do the job. See the items in red below:
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Registered User
    Join Date
    09-30-2011
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Using .SetFocus inside a TextBox_Exit event causes _Exit event to fire twice.

    Huh, looks like that would work! I think I tried something like that but couldn't really sort out when to turn the boolean true and when to turn it false. I can't really check if this would work because I ended up changing my approach in that now I just turn the duplicate textbox yellow to highlight and then clear the contents of the current textbox. That way it indicates to the user where the duplicate is without the current textbox loosing focus. I'll have to keep YOUR solution in mind for the future though, thanks!

    -Fatty

+ 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. ListBox Change Event doesn't fire
    By Michiel93 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-03-2015, 10:34 AM
  2. [SOLVED] Open Event to Fire During Specified Hour Only
    By dreicer_Jarr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2013, 05:11 AM
  3. [SOLVED] Fire A Change Event Only When a Cell = 1 in a Range
    By rye123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2013, 07:21 AM
  4. TextBox_Exit event doesn't trigger
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-03-2010, 05:08 PM
  5. can't get calculate event to fire vba
    By Cicada in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2010, 06:47 AM
  6. [SOLVED] Event doesn't fire
    By Frank Xia in forum Excel General
    Replies: 6
    Last Post: 02-10-2006, 09:00 PM
  7. Fire Event only when Cell Change?
    By HotRod in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-28-2005, 04:06 PM

Tags for this Thread

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