How to set the cursor at the end of a textbox after it was SetFocus'ed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael R
    New Member
    • Nov 2006
    • 176

    How to set the cursor at the end of a textbox after it was SetFocus'ed?

    Hi everyone.

    I wanna say first, I appritiate a lot the fact that I'm able to ask questions here!

    I have a form with a textbox that if its value equals to a certain condition, a command button appears next to it. After the command button appears I need the cursor to go back to the last letter of the textbox so that the user could continue typing without interruption.

    I use OnChange event for that:
    Code:
    Private Sub CustomerName_Change()
        Me.Refresh
        If IsNull(DLookup("CustomerId", "tblCustomers", "CustomerName='" & Me.CustomerName & "' And CustomerSirName = '" & Me.CustomerSirName & "'")) Then
            Call subResizeFmeCustomer(1)
            Me.CustomerName.SetFocus
        End If
    End Sub
    With the aforementioned code however, I get the whole textbox value being selected, so if the user will type anything, the value will be deleted.

    Using
    Code:
    SendKeys "{END}"
    or
    Code:
    SendKeys "{LEFT}"
    at the end of that code doesn't do anything.

    I wonder if there's anyway to go to the last letter of a textbox value after that textbox value has been SetFocus'ed.

    Thanks for your time!!

    Michael.
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    I have to say I'm not sure about exactly what you're trying to do here, but this should set the focus on CustomerName then move the cursor to the end of the data, without anything being selected:

    Code:
    Me.CustomerName.SetFocus
    Me.CustomerName.SelStart = Nz(Len(Me.CustomerName), 0)
    Me.CustomerName.SelLength = Nz(Len(Me.CustomerName), 0)
    Linq ;0)>

    Comment

    • Michael R
      New Member
      • Nov 2006
      • 176

      #3
      Originally posted by missinglinq
      I have to say I'm not sure about exactly what you're trying to do here, but this should set the focus on CustomerName then move the cursor to the end of the data, without anything being selected:

      Code:
      Me.CustomerName.SetFocus
      Me.CustomerName.SelStart = Nz(Len(Me.CustomerName), 0)
      Me.CustomerName.SelLength = Nz(Len(Me.CustomerName), 0)
      Linq ;0)>
      Works like a charm, thanks!

      (I was trying to make a certain button appear only when CustomerName fits a certain condition)

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Coming in late:
        Code:
        Me![CustomerName].SetFocus
        Me![CustomerName].SelStart = IIf(IsNull(Me![CustomerName]), 0, Len(Me![CustomerName]))

        Comment

        • missinglinq
          Recognized Expert Specialist
          • Nov 2006
          • 3533

          #5
          Better late than never!

          Linq ;0)>

          Comment

          Working...