By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

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

100+
P: 176
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerName_Change()
  2.     Me.Refresh
  3.     If IsNull(DLookup("CustomerId", "tblCustomers", "CustomerName='" & Me.CustomerName & "' And CustomerSirName = '" & Me.CustomerSirName & "'")) Then
  4.         Call subResizeFmeCustomer(1)
  5.         Me.CustomerName.SetFocus
  6.     End If
  7. End Sub
  8.  
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
Expand|Select|Wrap|Line Numbers
  1. SendKeys "{END}"
or
Expand|Select|Wrap|Line Numbers
  1. 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.
Oct 4 '08 #1
Share this Question
Share on Google+
4 Replies


missinglinq
Expert 2.5K+
P: 3,532
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:

Expand|Select|Wrap|Line Numbers
  1. Me.CustomerName.SetFocus
  2. Me.CustomerName.SelStart = Nz(Len(Me.CustomerName), 0)
  3. Me.CustomerName.SelLength = Nz(Len(Me.CustomerName), 0)
  4.  
Linq ;0)>
Oct 4 '08 #2

100+
P: 176
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:

Expand|Select|Wrap|Line Numbers
  1. Me.CustomerName.SetFocus
  2. Me.CustomerName.SelStart = Nz(Len(Me.CustomerName), 0)
  3. Me.CustomerName.SelLength = Nz(Len(Me.CustomerName), 0)
  4.  
Linq ;0)>
Works like a charm, thanks!

(I was trying to make a certain button appear only when CustomerName fits a certain condition)
Oct 4 '08 #3

ADezii
Expert 5K+
P: 8,610
Coming in late:
Expand|Select|Wrap|Line Numbers
  1. Me![CustomerName].SetFocus
  2. Me![CustomerName].SelStart = IIf(IsNull(Me![CustomerName]), 0, Len(Me![CustomerName]))
  3.  
Oct 4 '08 #4

missinglinq
Expert 2.5K+
P: 3,532
Better late than never!

Linq ;0)>
Oct 4 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.