Results 1 to 15 of 15
  1. #1
    Join Date
    May 2007
    Posts
    74

    Unanswered: OnLostFocus doesn't work properly

    I have a test box with code attached to the .OnLostFocus event.

    When I leave the text box, the OnLostFocus event code doesn't get called.

    However, the same .OnLostFocus type of event works just fine for another text box on the same subform.

    Does anybody have an idea of what is going wrong here?

    Thanks!
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    What happens if you put in code like: msgbox "Lost focus" in the event? Can you post your code for the .OnLostFocus event? Are you sure the text box got the focus (maybe put in a msgbox "Got focus" in the OnGotFocus event.)
    Last edited by pkstormy; 06-08-07 at 22:37.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    May 2007
    Posts
    74
    Quote Originally Posted by pkstormy
    What happens if you put in code like: msgbox "Lost focus" in the event? Can you post your code for the .OnLostFocus event? Are you sure the text box got the focus (maybe put in a msgbox "Got focus" in the OnGotFocus event.)
    My code within the .OnLostFocus event was a single subroutine call.

    The way that I found out there was a problem was that I set a breakpoint on the "Private Sub txtMyMessedUpTextBox_LostFocus" subroutine entry point, and that subroutine entry point isn't being hit when I leave the text box.

    The subroutine call within the txtMyMessedUpTextBox_LostFocus subroutine isn't being called, either.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  4. #4
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    If you do this...

    Private Sub MyTextBox_LostFocus()
    msgbox "Lost Focus"
    ...
    your sub routine call here or other code
    ...
    End Sub

    Does it show the message box? Maybe I'm misunderstanding your question.
    Last edited by pkstormy; 06-09-07 at 00:09.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Can you post your code for the LostFocus or your routine?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I've seen this sort of problem before, where the Access runtime (Ie when viewing the form) doesn't seem to generate the correct messages.

    The code is there but for some reason the event hook in the controls property isn't selected. When that happens you can clear the fault by doing a compact & rebuild the app, compile the app or select the event hook in the controls properties.

    as to what causes it I don't know
    I could be that sometimes writing code directly doesn't get associated with the controls event message
    it could be that a compile error stops the VB editor correctly updating everything.

    I take it you have put a message box as the first line of the event procrdure to prove that the event is/isn't being called, after all its perfectly feasible that the event may be running per4fectly.. it just may not be doing what you expect it to.
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I could be that sometimes writing code directly doesn't get associated with the controls event message
    This is a good point, Healdem! I don't remember ever seeing this from simply entering the code in a sub by using the dropdown box in the VBE, but I've seen it a number of time when copying and pasting code into the module. You have to go back out to Design View, select the control then click on the appropriate property to take you back into the VBE. That seems to "hook" the control to the code.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Yes, good point. I've had this happen to me also. If I copy code or write it directly, I get in the habit of going back to the event property and reselecting the event procedure, then the ..., and then debugging, compacting and repairing. I assumed azjazz has done this. If this doesn't work, I then throw in a msgbox into the event to see if it is actually triggered or if there is some other reason it isn't called (i.e. an IF, bad name, or some other statement which is causing the event to be bypassed.)
    Last edited by pkstormy; 06-09-07 at 11:57.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  9. #9
    Join Date
    May 2007
    Posts
    74
    Thanks for responding, guys ... Sorry I didn't get back to you sooner, but I didn't work this weekend.

    Putting the MsgBox in the routine proved that the routine associated with the one of the text boxes isn't getting called. The MsgBox didn't appear.

    My code is really simple right now:

    Code:
    Private Sub txtFilePath_LostFocus()
        
        MsgBox ("txtFilePath")
        setFieldState
    
    End Sub
    
    Private Sub txtRev_LostFocus()
        
        MsgBox ("txtRev")
        setFieldState
    
    End Sub
    I also previously tried the reassociation with the Properties >> On Lost Focus selections, but saw no effect from that.

    Another odd thing about this is that I have two text boxes, and the problem seems to alternate between the two boxes. Sometimes, the txtRev box shows this problem and txtFilePath works OK, and sometimes the txtFilePath shows this problem while txtRev works fine.

    Darned confused.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    just wondering if you've got similar events firing at the same time and the access runtime environment is struggling to work out which events should be served. sometimes windows messages can get lost, or they can arrive out of sequence.
    I'd rather be riding on the Tiger 800 or the Norton

  11. #11
    Join Date
    May 2007
    Posts
    74
    Quote Originally Posted by healdem
    just wondering if you've got similar events firing at the same time and the access runtime environment is struggling to work out which events should be served. sometimes windows messages can get lost, or they can arrive out of sequence.
    Hi, healdem -

    How would this happen? And if it is, can I prove it somehow?
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

  12. #12
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Sounds to me like you have to refresh your event link

    Go to the control - right click properties - events tab.
    Scroll to "OnLostFocus" and click the dotted button and build event.
    George
    Home | Blog

  13. #13
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    on post #9 azjazz claims to have already checked that....

    one of the bugbears in the windows world is that you cannot EVER rely on the events arriving in exactly the same sequence as they fired (its rare, its unlikely but it can happen), and bearing in mind that Access provides a superset of events ontop of windows events you have the possibility of a real pigs ear. In my view events should be used when appropriately, but like domain functions sparingly.

    Some of the minor events can be over taken by larger / significant events so its possible that a record update event may take precedence over an on lost focus control event
    a form on lost focus event may have priority over a control lost focus event.

    personally the only times I've seen what you are experiencing is as advised in post #6, and usually because Id written the functions directly in the code window without going through the form designer / GUI. In all of those cases the appropriate event wasn't ticked in the form GUI, ticking the event cleared the fault. A few years ago I saw the fault occur but everything seemed OK, until I tried a full compile, which thew up some errors, once those were cleared it worked fine. Ive also done a compact and repair occasionally to clear this type of fault.

    There is another option... which I hvae seen very rarely.. and that is when the Access model is banjaxed... You have code that you knwo works, it compiles OK, it looks OK but it doens't seem to run, the code that is running is a previous version. fundamentally what access is doing is not what the code says it should be doing. Ive put this down to access using an old internal copy or PCode version and isn't using hte current code forwahtever internal reason. in this event I'd try a decompile, or rebuild the changes form a backup copy
    Last edited by healdem; 06-12-07 at 06:57.
    I'd rather be riding on the Tiger 800 or the Norton

  14. #14
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    When I experienced events not working, it was usually the result of 2 events procedures which had the same name.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  15. #15
    Join Date
    May 2007
    Posts
    74
    healdem - I will try re-associating the events to the code, but I think there is already an association of some sort, since the events do actually call the right subroutines occasionally. I'll admit that I may have been guilty of just typing in the subroutines instead of using the GUI. My somewhat impatient nature may have gotten the better of me.

    Paul - I checked my code, and there weren't any duplicate entries for those subroutine names in the code associated with that module. I wouldn't think that there are any "public" events in the code from other subforms, because that would be kinda stoopid - though I did inherit this code from other developers that could have developed this tool better.
    AzJazz

    "I'm afraid the holodeck will be society's last invention." - Scott Adams

Posting Permissions

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