Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cursor changes position in Memo field 1

Status
Not open for further replies.

hurtsmyhead

Technical User
Jun 3, 2010
8
Wazzup everybody,

I got the code below from another thread--it's from MajP and it works great, with one little issue...

I'm using it on double click to add a small bit of text to a Memo field on my form, right at the cursor position where I'm double clicking. Sometimes the total amount of text in the memo field is larger than the physical size I've allotted for it on my form, so it scrolls. In this situation, when I'm scrolled down to one of the lines at or near the bottom and double click to add the text, the text adds just fine but the cursor shoots back to or near the top of the field and and the field has scrolled back all the way (I can see the first line of text again).

This behavior does not occur if I'm using it on a line that's above the point where I have to scroll--the cursor stays right where I just double clicked. Hope that makes sense. Is there any way to stop this scrolling up behavior and keep the cursor where it was?


Private Sub Details_DblClick(Cancel As Integer)
Dim strTextbegin As String
Dim strTextEnd As String
Dim strTextInsert As String
Dim pos As Integer
strTextInsert = "<br />"
Dim ctl As Access.TextBox
Set ctl = Details
ctl.SetFocus
pos = ctl.SelStart
strTextbegin = Left(ctl.Text, pos)
strTextEnd = Mid(ctl.Text, pos + 1)
ctl.Text = strTextbegin & strTextInsert & strTextEnd
End Sub
 
Private Sub memFld_DblClick(Cancel As Integer)
Dim strTextbegin As String
Dim strTextEnd As String
Dim strTextInsert As String
Dim pos As Integer
Dim returnPos As Integer
strTextInsert = "<br />"
Dim ctl As Access.TextBox
Set ctl = Me.memFld
ctl.SetFocus
pos = ctl.SelStart
strTextbegin = Left(ctl.Text, pos)
returnPos = Len(strTextbegin)
strTextEnd = Mid(ctl.Text, pos + 1)
ctl.Text = strTextbegin & strTextInsert & strTextEnd
ctl.SelStart = returnPos
End Sub
 
Thank you MajP, I appreciate it. Obviously, I'm no kind of Access developer, just a long time IT guy trying to create a database for my side business.

I did spend time since I started this thread understanding this code--it actually rewrites the contents of the field, inserting the additional text at the cursor, correct?

Let me ask you one thing--couldn't I have just used "pos" as my final cursor position? Does "returnPos" put the cursor in a different location than "pos" does?

Thanks again, I'm so glad I found this forum.

Mark
 
You are correct. For some reason I tried that first and it was not working. But I retried and it is fine

Private Sub memFld_DblClick(Cancel As Integer)
Dim strTextbegin As String
Dim strTextEnd As String
Dim strTextInsert As String
Dim pos As Integer
Dim ctl As Access.TextBox

Set ctl = Me.memFld
strTextInsert = "<br />"
ctl.SetFocus
pos = ctl.SelStart
strTextbegin = Left(ctl.Text, pos)
strTextEnd = Mid(ctl.Text, pos + 1)
ctl.Text = strTextbegin & strTextInsert & strTextEnd
ctl.SelStart = pos
End Sub
 
Ok, maybe I am learning a little, LOL. My two partners who are doing data entry are loving the change.

New guy question (since I just joined yesterday)--earlier I thought I saw a way to close a thread but I don't see it now. Am I correct in assuming this is a necessary bit of housekeeping, since my issue has been resolved?

Mark
 
No you do not need to close a thread. It is always helpful to other members and polite to say if it resolved the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top