Results 1 to 6 of 6
  1. #1
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496

    a control still has focus at the LostFocus event (?)

    this has always perplexed me...

    on a control's LostFocus() event, i have this code:
    me.control_name.visible = False
    which results in the run-time error 2165: 'You cannot hide a control that has focus'

    -->but didn't the control just loose focus (?)

    ______________________________________


    background:
    i have a control that becomes visible based on user choices (at other controls.) i am looking to hide that control if the user clicks ANYWHERE else on the form (i.e. chooses to ignore this control - which is OK). The form has a lot of controls, so putting code behind each of their OnFocus event to close this one control is not really a practical approach. Thought about putting a timer on it, but that seems a bit heavy handed (maybe?)

    as always, with appreciation in advance,
    m.

  2. #2
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    I think the issue is at the time the code runs the control is technically "Losing focus" but hasn't actually lost it at that point?
    According to Microsoft though that isn't the case allegedly...
    https://support.microsoft.com/en-us/...86553682f9#bm1

    You could try setting focus to a specific control and then hide the one you want to hide?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Disable the control first, then you can hide it whilst it still has focus. See Click Me And I Disappear! (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    Quote Originally Posted by isladogs View Post
    Disable the control first, then you can hide it whilst it still has focus. See Click Me And I Disappear! (isladogs.co.uk)
    YEAH! with one minor adjustment:
    the control needs to be re-enabled after it is hidden
    (seems counter-intuitive to me... but it works):

    'first disable the control then hide it
    Me.control_name.Enabled = False
    Me.control_name.Visible = False
    Me.control_name.Enabled = True

    thnx!

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    I don't find it necessary to re-enable the control. Why do you need to do so?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Another option is to shift the focus to another control before hiding. I recently noticed, not sure if it existed in earlier versions of access, that command buttons have a transparent property.
    I place a button "ctlFocus" on the form and set transparent to yes. Then in my code I use me.ctlFocus.SetFocus before setting anything invisible.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. On Lost Focus Event
    By data808 in forum Access
    Replies: 4
    Last Post: 12-03-2022, 04:25 AM
  2. Replies: 7
    Last Post: 03-09-2018, 11:11 PM
  3. Replies: 15
    Last Post: 05-12-2016, 02:27 PM
  4. Replies: 10
    Last Post: 03-24-2016, 03:11 PM
  5. User focus control in AfterUpdate event
    By nosliwmada@gmailcom in forum Programming
    Replies: 3
    Last Post: 12-20-2010, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums