place cursor at end of first line in memo field (1 Viewer)

easykey

New member
Local time
Today, 00:31
Joined
Dec 2, 2014
Messages
6
I have created a button that takes the contents of a memo field and adds a new line of text at the top with the date, time and initials of staff member. So far, so good...

However I want it to end with the cursor ready at the end of the first line (to type the note) The following code does everything but ends by putting the cursor right at the end of all of the text in the memo field (instead of the end of the first line):

Code:
Private Sub Command59_Click()
Me.Note = Format(Now, "d mmm yyyy hh:nn") & " - " & "(" & DLookup("[login]", "logintable") & ") " & Chr(13) & Chr(10) & Me.Note
    Me.Note.SetFocus
    Me.Note.SelStart = Len(Me.Note)
    Me.Note.SelLength = 0
End Sub
 

AccessBlaster

Registered User.
Local time
Yesterday, 16:31
Joined
May 22, 2010
Messages
5,911
Code:
Me.Note.SetFocus

File > Options > Client Settings > Editing

 

Attachments

  • Options.png
    Options.png
    72.8 KB · Views: 718

easykey

New member
Local time
Today, 00:31
Joined
Dec 2, 2014
Messages
6
Thanks AccessBlaster but that doesn't really help me.

I have a Memo field called Notes that can hold a few lines of text I need it to do the following:
1. Copy all of the current contents (this works)
2. Paste all of the contents inserting a new line of text at the top (this works)
3. Put today's date and time plus the staff initials on that new top line (this works)
4. Place the cursor at the end of the first line - ready to type appending text to the end of the top line but above any earlier lines (this does not work) the code above puts the cursor at the very end of all lines of text in the memo box.

This second attempt kinda works (but only if steps 1, 2 and 3 creates exactly the right number of characters) the problem is that the number varies.

Code:
Private Sub Command59_Click()
Me.Note = Format(Now, "d mmm yyyy hh:nn") & " - " & "(" & DLookup("[login]", "logintable") & ") " & Chr(13) & Chr(10) & Me.Note
    Me.Note.SetFocus
    Me.Note.SelStart = 25
    Me.Note.SelLength = 0
End Sub

I guess I could do with a way of counting the number of characters created in steps 1, 2 and 3.

Or maybe a way of counting the number of characters up to the first line break?
 

AccessBlaster

Registered User.
Local time
Yesterday, 16:31
Joined
May 22, 2010
Messages
5,911
How would you define the end of the first line? Is it a period? If so maybe the Instr Function will help.
 

MarkK

bit cruncher
Local time
Yesterday, 16:31
Joined
Mar 17, 2004
Messages
8,179
You can assign the first line to a variable, then insert it, then check the length of it and advance the insertion point . . .
Code:
Private Sub Command59_Click()
    Dim tmp As String
    tmp = [COLOR="DarkRed"]Format(Now, "d mmm yyyy hh:nn") & " - " & "(" & DLookup("[login]", "logintable") & ") " & vbCrLf[/COLOR]
    Me.note = [COLOR="Blue"]tmp & Me.note[/COLOR]
    Me.note.SetFocus
    Me.note.SelStart = [COLOR="Green"]Len(tmp)[/COLOR]
    Me.note.SelLength = 0
End Sub
You can also seach for chr(13) & chr(10), which is the same as the VBA constant vbCrLf.
Code:
    Me.note.SelStart = InStr(1, Me.Note, vbCrLf)
 

easykey

New member
Local time
Today, 00:31
Joined
Dec 2, 2014
Messages
6
Brilliant! It needed a slight adjustment as when I start typing where the cursor was placed it pre-pended it to the beginning of the second line.

So here is the finished code that works:

Code:
Private Sub Command83_Click()
Me.Note = Format(Now, "d mmm yyyy hh:nn") & " - " & "(" & DLookup("[login]", "logintable") & ") " & Chr(13) & Chr(10) & Me.Note
    Me.Note.SetFocus
    Me.Note.SelStart = InStr(1, Me.Note, vbCrLf) - 1
    Me.Note.SelLength = 0
End Sub

Thank you so much :)
 

Users who are viewing this thread

Top Bottom