+ Reply to Thread
Results 1 to 3 of 3

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

Hybrid View

  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:
    Private Sub Batch_Job01_Exit(ByVal Cancel As MSForms.ReturnBoolean)   'I have this code for all 30 textbox_exit events
        Call JobPull(Batch_Job01.Value, 1)
    End Sub
    
    Private Sub JobPull(Job, JobNum)
        Dim JobField As Object
        Dim DupPos As integer
        Dim Result As Boolean
    
        Call DuplicateJobCheck(JobNum, DupPos, Result)
        'JobNum is an input, DupPos and Result are outputs. 
        'DupPos gives the job number of the duplicate job, 
        'and Result is either true or false depending on whether there is a duplicate.
    
        If Result = False then
            'code to pull in job information off our server
          else
            MsgBox ("Duplicate Job")
            If DupPos <10 then Set Jobfield = Me.Controls("Batch_PartNumber0" & DupPos)
            If DupPos > 9 then Set Jobfield = Me.Controls("Batch_PartNumber" & DupPos)
            JobField.SetFocus   '<<---THIS IS WHERE THE PROBLEM IS. I can't figure out how to KEEP the focus here after the _Exit event finishes firing.
        End If
    
    End Sub
    
    Sub DuplicateJobCheck(JobNum, DupPos, Result)
        
        'Dim and Set Form control variables
    
        'For loop to test each Job field's contents
        'If contents match, Result = True, DupPos = i
    
    End Sub
    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:
    Option Explicit
    
    Private bGblDisableUserFormEvents As Boolean
    
    Private Sub Batch_Job01_Exit(ByVal Cancel As MSForms.ReturnBoolean)   'I have this code for all 30 textbox_exit events
        If bGblDisableUserFormEvents = False Then
            Call JobPull(Batch_Job01.Value, 1)
        End If
    End Sub
    
    Private Sub JobPull(Job, JobNum)
        Dim JobField As Object
        Dim DupPos As Integer
        Dim Result As Boolean
    
        Call DuplicateJobCheck(JobNum, DupPos, Result)
        'JobNum is an input, DupPos and Result are outputs.
        'DupPos gives the job number of the duplicate job,
        'and Result is either true or false depending on whether there is a duplicate.
    
        If Result = False Then
            'code to pull in job information off our server
          Else
            MsgBox ("Duplicate Job")
            If DupPos < 10 Then Set JobField = Me.Controls("Batch_PartNumber0" & DupPos)
            If DupPos > 9 Then Set JobField = Me.Controls("Batch_PartNumber" & DupPos)
            bGblDisableUserFormEvents = True
            JobField.SetFocus   '<<---THIS IS WHERE THE PROBLEM IS. I can't figure out how to KEEP the focus here after the _Exit event finishes firing.
            bGblDisableUserFormEvents = False
        End If
    
    End Sub
    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. Event doesn't fire
    By Frank Xia in forum Excel General
    Replies: 6
    Last Post: 02-10-2006, 09:00 PM
  7. [SOLVED] 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