Go to the end of a memo field on SetFocus

travismp

Registered User.
Local time
Today, 15:46
Joined
Oct 15, 2001
Messages
386
I have a memo field that holds a lot of data for almost every client. When I open my main form the first field that is active is my memo field however it highlights all of the current data. I would rather it go to the end of the memo field on SetFocus. So can I write some VB or change some property some where that when you SetFocus on a field it will automatically go to the end of the field?
 
Do the set focus first

Me.YourControlName.SetFocus

and then

Me.YourControlName.SelStart = Len(Me.YourControlName)
Me.YourControlName.SelLength = 0
 
that did it. Thank you so much my friend. You are the man.
 
If the field gets over 32000 characters then change to

SendKeys "{F2}", True
 
Good point Mike - because SelStart is an Integer and anything over 32,767 will cause an overflow error.
 
A quick follow up. I need to change this just a little bit. Here is my exact script so far that launches on "On Got Focus" of my field inside the form

Option Compare Database

Private Sub COMMENTS_GotFocus()

Me.COMMENTS.SelStart = Len(Me.COMMENTS)
Me.COMMENTS.SelLength = 0

End Sub


I need to add something to check to make sure there is even data listed before it tries to go to the end. Right now if I pull up a company with no comments I will get a

Run-time error '94'
Invalid Use of Null
Debug Window.

So can I add something that says

IF COMMENTS Is Null THEN
do nothing
ELSE
Me.COMMENTS.SelStart = Len(Me.COMMENTS)
Me.COMMENTS.SelLength = 0
END IF
 
Code:
Private Sub COMMENTS_GotFocus()
 If Not IsNull(Me.COMMENTS) Then
  Me.COMMENTS.SelStart = Len(Me.COMMENTS)
  Me.COMMENTS.SelLength = 0
 End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom