Results 1 to 5 of 5
  1. #1
    stainl77 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    5

    Losing focus from textbox after On Change event

    On my form, I have an On Change event for a textbox that calls a macro that in turn calls a function.

    This all seems to work fine regarding running the macro and code. however, when the code is completed, the textbox on the form has lost focus and with that, also position in the box. I mention that because I tried to fix with a SetFocus in the code but obviously this simply gave focus to the textbox with no memory of where the cursor was. i WANT TO BE ABLE TO ADD A CONTINUouS STRING OF TEXT OR DELETeS, ETC WITHOUT any sign that code is being run in the background. Hope that makes sense. Just want smooth text input/amendments without losing position or focus.

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Not sure you will ever get smoothness you ask for. Do you realize that event fires for every keystroke? So if I want to add "new" to the end, it has just run 3x. I guess it all depends on what the called code is doing. Are you sure you would not be better off with some other event, such as AfterUpdate? Regardless, if you are asking how to get the cursor to the end of the entry, you need to get the length of it's Text property after setting focus, then set the SelStart property to that number.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    stainl77 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    5
    i was trying to be a bit too smart and do things such as only enable an Add to List button when text has been changed to something different to previously saved text in a listbox on the form, not on a database yet. So if New had been previously added to this listbox using textbox1 and the Add to List button, it would only be enabled if the textbox1 was populated with something different to New in the list, such as New2. Then next pass, it would check against New and New2 to enable the button. This would have allowed a list to be built up on the form and the button only enabled when the text was different. I liked the idea of this happening in real time but guess I need to go with AfterUpdate instead and validate at that point, with the button always enabled. There is only one user so calling simple code several times would be unlikely to have performance issues. Thanks for the info. I will look at the AfterUpdate.

    Just for completeness, working out the length of text would not help much as they could have just overwritten character 2 of a 10 letter word, so cursor could jump about if I tried using that for positioning. I may have a mess around with SelcectionStart values before I give up on this entirely. Might help position. You never know

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    Edit - opened this thread ages ago so have not seen subsequent replies

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    so cursor could jump about if I tried using that for positioning
    Only because you would be using the wrong event. Now that you've explained what's going on, I suggest you take a look at the combo box Not In List event rather than having a listbox and a button to do what the combo alone can do with that event. If you must have the listbox for some reason, then after the not in list event finishes, you can requery the listbox if a value was added. If you stick with your current design, then form (or perhaps control) BeforeUpdate would be more appropriate than AfterUpdate. You can cancel and undo an entry if it already exists, but only before the update happens, which makes more sense than allowing a new item then having to remove it because it's already there.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 9
    Last Post: 04-15-2021, 09:20 AM
  2. Issues with change event of textbox
    By udigold1 in forum Programming
    Replies: 4
    Last Post: 03-27-2019, 08:39 AM
  3. Replies: 2
    Last Post: 11-24-2018, 09:37 AM
  4. Requery without losing focus?
    By Axeia in forum Programming
    Replies: 3
    Last Post: 05-11-2011, 04:53 PM
  5. Any change of focus on form Event?
    By trb5016 in forum Forms
    Replies: 1
    Last Post: 02-11-2011, 08:25 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