Place curser at end of line?

M

Macsmasher

Hi everybody,

Running Access07

I have a memo field that I will use as a journal. There is a cmd button
that I want to place the curser at the beginning of any existing text,
insert two lines, insert the UserName and date, then place the curser at the
end of that new line so
the User can begin typing. The below code works fine with the exception for
placing the curser at the end of the line. Since using SendKeys "{END}" is
never a good idea, I need either something to replace it, or a suggestion on
how to do this differently.

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
' replace SendKeys "{END}" with what???
End With

Thanks in advance for your input!

-Larry
Maximize Software, Inc.
 
K

Klatuu

You use the selstart property to position the cursor at any point in the
text. You are using ": " to terminate your current entry, so you would want
to position it right after that, so it would look like
<> blank line
<> blank line
Fred 07/23/2009: |
cursor here ---^
Here is all you need:

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
.SelStart = Instr(Me.memAdminNote, ": ")
End With
 
M

Macsmasher

Hi Dave,

Thank you for the response, but what you suggested placed the curser down in
the first line of existing text below the two inserted lines. However, the
following works great:

Dim strUser As String
Dim strDateStamp As String

strUser = Forms!frmSwitchboard!txtCurrentUser
strDateStamp = strUser & " " & Date & ": "

With Me.memComments
.Locked = False
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strDateStamp
.SelStart = Len(strDateStamp)
.SelLength = 0
End With


Klatuu said:
You use the selstart property to position the cursor at any point in the
text. You are using ": " to terminate your current entry, so you would want
to position it right after that, so it would look like
<> blank line
<> blank line
Fred 07/23/2009: |
cursor here ---^
Here is all you need:

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
.SelStart = Instr(Me.memAdminNote, ": ")
End With

--
Dave Hargis, Microsoft Access MVP


Macsmasher said:
Hi everybody,

Running Access07

I have a memo field that I will use as a journal. There is a cmd button
that I want to place the curser at the beginning of any existing text,
insert two lines, insert the UserName and date, then place the curser at the
end of that new line so
the User can begin typing. The below code works fine with the exception for
placing the curser at the end of the line. Since using SendKeys "{END}" is
never a good idea, I need either something to replace it, or a suggestion on
how to do this differently.

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
' replace SendKeys "{END}" with what???
End With

Thanks in advance for your input!

-Larry
Maximize Software, Inc.
 
K

Klatuu

hhhmmm, worked for me, but I like your idea better anyway.
--
Dave Hargis, Microsoft Access MVP


Macsmasher said:
Hi Dave,

Thank you for the response, but what you suggested placed the curser down in
the first line of existing text below the two inserted lines. However, the
following works great:

Dim strUser As String
Dim strDateStamp As String

strUser = Forms!frmSwitchboard!txtCurrentUser
strDateStamp = strUser & " " & Date & ": "

With Me.memComments
.Locked = False
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strDateStamp
.SelStart = Len(strDateStamp)
.SelLength = 0
End With


Klatuu said:
You use the selstart property to position the cursor at any point in the
text. You are using ": " to terminate your current entry, so you would want
to position it right after that, so it would look like
<> blank line
<> blank line
Fred 07/23/2009: |
cursor here ---^
Here is all you need:

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
.SelStart = Instr(Me.memAdminNote, ": ")
End With

--
Dave Hargis, Microsoft Access MVP


Macsmasher said:
Hi everybody,

Running Access07

I have a memo field that I will use as a journal. There is a cmd button
that I want to place the curser at the beginning of any existing text,
insert two lines, insert the UserName and date, then place the curser at the
end of that new line so
the User can begin typing. The below code works fine with the exception for
placing the curser at the end of the line. Since using SendKeys "{END}" is
never a good idea, I need either something to replace it, or a suggestion on
how to do this differently.

strUser = Forms![frmSwitchboard]![txtCurrentUser]
With Me.memAdminNote
.SetFocus
.SelStart = 0
.SelText = vbNewLine & vbNewLine
.SelText = strUser & " " & Date & ": "
' replace SendKeys "{END}" with what???
End With

Thanks in advance for your input!

-Larry
Maximize Software, Inc.
 
L

Linq Adams via AccessMonster.com

Be aware that SelLength and SelStart both take an Integer as an argument
(which is limited to 32,767) but a Memo field can hold twice that many
characters (actually 4 times that, I understand, if the data is entered via
code) so you have to be careful using them with these fields! I can't imagine
selecting an entire memo field, but if you wanted to, say, place the cursor
at the end of the field, as you are doing here, you should test it's length
and if Len(strDateStamp) > 32767 set it to 32767, which would at least get
you a whole lot closer to the end of the data.

So if there's any chance of your memo field running past this limit you need
to replace the line

SelStart = Len(strDateStamp)

with

If Len(strDateStamp) > 32767 Then
strDateStamp.SelStart = 32767
Else
strDateStamp.SelStart = Len(strDateStamp)
End If
 
K

Klatuu

Good point, Linq. In this case; however, the OP is not wanting to select the
entire field, but at which point in the text to position the cursor. The
only use of the SelLength is to set it to 0 so the text from the beginning to
the cursor is not selected (highlighted)
 
L

Linq Adams via AccessMonster.com

I understand that; as I said, I can't imagine selecting an entire memo field,
but just threw that in as SleLength is a related function to SleStart and has
the same limitations.
 
L

Linq Adams via AccessMonster.com

I understand that; as I said, I can't imagine selecting an entire memo field,
but just threw that in as SelLength is a related function to SelStart (which
is important to the OP) and has the same limitations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top