Results 1 to 4 of 4
  1. #1
    paddon is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    24

    Text Box Refocus

    Hello All,

    Simple VB question but I just cannot seem to figure out a glitch I am encountering in my code. I have a test box which operates as a Quick Search. When it changes I want Access to re-query the sub form in question and then reset focus on the text box at the END.

    It works until 2 letters are input then it each time it refocuses it puts the cursor in the middle of the word and I cannot figure out why. Here is the code.

    Code:
    Private Sub Search_Change()
        Dim vSearchString As String
        Dim ctlCombo As Control
        
        vSearchString = search.Text
        Search2.Value = vSearchString
    
        Set ctlQuery = Me.hsfProperties
        ctlQuery.Requery
        
        Me.search.SetFocus
        Me.search.SelStart = Len(Nz(Me.search, " "))
    End Sub
    The text box in Question is named "Search" and the subform is named hsfProperties.



    Any thoughts?

    Thanks in advance
    Paddon
    Last edited by paddon; 02-02-2011 at 12:58 PM.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    doesn't "selstart" put the cursor at the point of the argument?? does it ALWAYS do it?? everytime??

    have you checked your options?? there's one in there that specifies what to do when entering a field, but I think it only applies to datasheets.

    the change() event only happens after focus is lost on the text box though. you know that, right?

  3. #3
    paddon is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    24
    ajetrumpet,

    Thanks for the reply. The code worked fine when it looked like this

    Code:
    Private Sub Search_Change()
        Dim vSearchString As String
       
        vSearchString = search.Text
        Search2.Value = vSearchString
    
        DoCmd.Requery
        
        Me.search.SetFocus
        Me.search.SelStart = Len(Nz(Me.search, ""))
    End Sub
    What would happen in that case was the user would begin typing something into the box and after each letter was entered the requery would execute fine and the cursor would end up at the end of what was currently typed into the field. Now I have a few subforms on a dashboard type form now and I don't want to requery everything because it causes it to be slow. I only want the specific subform that the quick search box is meant to search within to requery. Hence my reasoning for changing the code to the following.

    Code:
    Private Sub Search_Change()
        Dim vSearchString As String
        Dim ctlCombo As Control
        
        vSearchString = search.Text
        Search2.Value = vSearchString
    
        Set ctlQuery = Me.hsfProperties
        ctlQuery.Requery
        
        Me.search.SetFocus
        Me.search.SelStart = Len(Nz(Me.search, ""))
    End Sub
    Everything works fine EXCEPT the fact that the cursor is always placed after the first letter in the text box, after the user enters another letter. What I don't understand is why the first code works perfectly but the second doesn't.

  4. #4
    paddon is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Dec 2010
    Posts
    24
    I jsut figured it out while typing up that response...

    I just needed to do the following

    Code:
    Private Sub Search_Change()
        Dim vSearchString As String
        Dim ctlCombo As Control
        
        vSearchString = search.Text
        Search2.Value = vSearchString
        Set ctlQuery = Me.hsfProperties
        ctlQuery.Requery
        
        Me.search.SetFocus
        Me.search.SelStart = Len(Nz(Me.search.Text, "")) + 1
    End Sub
    It was so easy I totally should have thought of it. I apologize. Thanks very much for you help.

    Regards,
    Paddon

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

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2010, 04:31 PM
  2. How to vertically align text inside a text box ?
    By alexcalgary in forum Forms
    Replies: 2
    Last Post: 10-06-2010, 08:44 AM
  3. Replies: 15
    Last Post: 09-18-2010, 01:19 PM
  4. Geting value/text to ready text
    By timpepu in forum Reports
    Replies: 2
    Last Post: 05-20-2010, 12:12 AM
  5. Split text field into two text fields
    By Grant in forum Access
    Replies: 6
    Last Post: 01-31-2008, 05:52 AM

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