Consulting

Results 1 to 3 of 3

Thread: ComboBox: Lose Focus without Triggering AfterUpdate

  1. #1
    VBAX Contributor
    Joined
    Apr 2014
    Posts
    109
    Location

    ComboBox: Lose Focus without Triggering AfterUpdate

    Hello friends of VBA

    I have a search form that is populated by one of 6 different sources depending on a option group selection. (Eg. Names by First then Last, Names by Last then First, etc..) The option group is set to keep "Names by First then Last" as default. But while using the form, there are times when I begin typing and realize that I only know the last name of this individual, and need to select the second option. However, as I already typed into the ComboBox, selecting the second option triggers an
    AfterUpdate event of the ComboBox, and the code that follows is a selection and closing of the search form.

    I would like to find a way to stop the
    AfterUpdate event when the focus is lost. This should be a simple thing, but my head isn't working it out so I thought to give someone an easy one.

    Please note that the problem only exists if a user begins to type. Simply moving into the box, and then moving back to change options works fine. The moment the box becomes dirty, the loss of focus triggers an
    AfterUpdate event, even if the line is completely erased.

    What I tried:
    I noticed that if I backspace first, but not enough, the resulting letters are "not in list", which means I have to backspace completely. So I tried using a key press event that tests for the Backspace (Ascii = 8), using that to set a variable to true, which I then test for in the
    AfterUpdate event. This works okay, but it is clumsy and doesn't take into account that a user could erase in a different way: (Home/ Delete) or (Mouse Select/ Delete).

    I also tried to use the Click event instead of the
    AfterUpdate, thinking it would not be triggered by the lack of focus. It is so triggered.

    I had hoped the ComboBox events would be in this order: LostFocus, AfterUpdate.
    But even though the lost focus creates the AfterUpdate the actual order is the other way around.
    Orders like this: [ComboBox].BeforeUpdate, .AfterUpdate, .Click, .LostFocus, [Frame Option Group].Enter

    The BeforeUpdate seems plausible, but what do I test for? If typing has begun, the value in the textbox is legit. If it was all erased the value will be Null. That works.
    But what if the user doesn't erase anything, types 'J' for example, gets John Doe as the first item in the list, realizes they don't know the name and clicks above for the Lastname first option. The test for null will not work. It would work if I could test to see that the focus left the ComboBox, because that is legitimately what happened. But the focus is still shown to be on the ComboBox until the updates are all complete.

    Ideas?

    Thanks for your help?
    GaryJ

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    You appear to have tried most things to do with the combo events, so perhaps some lateral thinking is required.
    You could try adding a text box to the search form and set it's tab order to after the combo.
    Move the VBA code to close the form to the text box's Got Focus event.
    Now when you physically give focus back to the Frame Option Group it won't trigger the closing of the form

  3. #3
    VBAX Contributor
    Joined
    Apr 2014
    Posts
    109
    Location
    Thanks for the idea, OBP, but it adds the problem of not closing the form until I tab out of the ComboBox.

    I found a solution for 90% of it by adding this as the first line to the AfterUpdate Event:
    If IsNull(Me.Combo76) Then Exit Sub

    It was kind of accidental as I thought that I'd have to empty the ComboBox so as not to trigger a "Not in List". The only situation it doesn't cover is the chance that a user doesn't erase any of the combobox, and thus it is not null. The result closes the search form, calls the selected name to the form, and waits for the user. As it would have been the wrong name, the user can now open the search form again, choose the other option, and select another name.

    GaryJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •