Keep cursor at end of text (1 Viewer)

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
I have the vba code below on the Double Click Event of a text box on a form. The code enters the current date but I would like the cursor to move to the end of the date ready for the user to enter their task update notes. This works only if the user clicks beneath the last entry but if they click on any of the previous text entries then the the cursor doesnt stay at the end of the text. I know the code is working as I have a message box to confirm the position of the cursor but then it just jumps back to where the user double clicked.

Code:
Task_update.Value = Task_update.Value & vbCrLf & Format(Now(), "dd-mm-yy") & ": "
                  Me.Task_update.SelStart = Me.Task_update.SelStart + Me.Task_update.SelLength
        
MsgBox Me.Task_update.SelStart

How can I get the cursor to always stay at the end of the date that has just been inserted by the double click event?
 
Last edited:

missinglinq

AWF VIP
Local time
Today, 12:01
Joined
Jun 20, 2003
Messages
6,423
I'm having a hard time following your needs, exactly, but try replacing your last line (the messagebox line) with

Me.Task_update.SelStart = Len(Me.Task_update) + 2

Linq ;0)>
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
I'm having a hard time following your needs, exactly, but try replacing your last line (the messagebox line) with

Me.Task_update.SelStart = Len(Me.Task_update) + 2

Linq ;0)>

Apologies I probably didn't explain myself very well. The message box is only there to confirm that the code gets the correct position.

I have managed to solve the issue by adding the following line of code.

Code:
    Me!Task_update.SelStart = Me!Task_update.SelLength
 

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
Whilst that code will work, you can also change the default cursor position to go to the end of existing text. This is a setting in Access Options
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
Whilst that code will work, you can also change the default cursor position to go to the end of existing text. This is a setting in Access Options

Just out of interest where is that setting? Ive looked in the Access Options and cant find it. Im using Access 2013.
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
Just out of interest where is that setting? Ive looked in the Access Options and cant find it. Im using Access 2013.

Ah just found them under client settings which wouldn't have been any good for this exercise but thanks anyway.
 

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
Hi
Yes its under client settings but out of interest why wouldn't it have been any good for this exercise?
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
Hi
Yes its under client settings but out of interest why wouldn't it have been any good for this exercise?

Because I would have had to change the client settings for every user which is not practical.
 

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
Because I would have had to change the client settings for every user which is not practical.

OK - not by directly editing all computers in a multiuser environment e.g. on a network

However if you distribute your database to several users, I think that setting will 'stick' for all users of that database ....UNLESS users change the setting back again

Worth checking at least... and I'd be interested to know whether I'm right!
 

missinglinq

AWF VIP
Local time
Today, 12:01
Joined
Jun 20, 2003
Messages
6,423
...However if you distribute your database to several users, I think that setting will 'stick' for all users of that database...
Not unless something has changed in recent versions. This setting only applies to the particular installation of Access where the option is set...and it applies to all databases run under that installation.

Linq ;0)>
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
Not unless something has changed in recent versions. This setting only applies to the particular installation of Access where the option is set...and it applies to all databases run under that installation.

Linq ;0)>

Just tested this on Access 2010 & 2016 on 2 different machines

Linq is correct & I was wrong:eek:

If you change one database on a computer, all databases on that computer are affected.
BUT copy the database to another computer and the new cursor settings are NOT transferred

So forget that idea :eek:
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
I thought I had solved this issue but having got the code working in my development copy of the application I have implemented the same code in the live copy only to find it throws an error 2465 saying it cannot find field Task_Update. (screenshot attached)

The odd thing is I took a copy of the live application and made the code changes to that which are working but when I put the code into the live copy it throws the error.

I did not build the original system but am now responsible for developing it which is somewhat difficult understanding other developers coding.

The name of the control on the form is 'task update' (with a space) but the code in the double click event before I added my code refers to it as Task_update with an underscore and this was all working before I touched it.

I am baffled as to why the code worked when referring to the control with an underscore when the name has a space. The field name in the table is also the same as the control name (with a space) .

I have tried deleting the form from the live system and then importing the working one from my development copy but still it produces an error on the double click event.

I am really stumped on this one.

Hope someone can help shed some light on the issue. Could this be a bug in Access 2013?

Code:
Private Sub Task_update_DblClick(Cancel As Integer)

        If Len(Task_update & vblnullstring) = 0 Then
                Task_update.Value = Format(Now(), "dd-mm-yy") & ": "
        Else
                Task_update.Value = Task_update.Value & vbCrLf & Format(Now(), "dd-mm-yy") & ": "
        End If
        
        Me.Task_update.SelStart = Me.Task_update.SelStart + Me.Task_update.SelLength
        [COLOR="Red"]Me!Task_update.SelStart = Me!Task_update.SelLength[/COLOR]

End Sub


The line of code in red above is my code and this is working in my dev copy but throws the error at this line of code.

If I change my code to either of the lines below then I no longer get an error but the cursor then doesnt move to the end of the string of text.

Code:
Me.Task_update.SelStart = Me.Task_update.SelLength

Code:
Me![Task update].SelStart = Me![Task update].SelLength
 

Attachments

  • Error 2465.JPG
    Error 2465.JPG
    17.1 KB · Views: 104
Last edited:

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
In the code, just write it in square brackets and without the underscore if that's not in the original

Code:
Me.[Task Update]

Don't modify the procedure name however
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
In the code, just write it in square brackets and without the underscore if that's not in the original

Code:
Me.[Task Update]

Don't modify the procedure name however

I changed the line of code to

Code:
Me.[Task update].SelStart = Me.[Task update].SelLength

but although it no longer errors it doesn't actually move the cursor to the end of the text ready for the user to start typing their notes. The cursor just stays at the position you did the double click.

Still baffled as to why it works in my dev copy and not in the live version when the dev version is a copy of the live one.
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
You should have changed all 9 references in the procedure to [Task update]
See if that fixes it.

In case it helps, see the solution posted by JHB in my parallel thread which was triggered by yours....

https://www.access-programmers.co.uk/forums/showthread.php?t=295901

I changed it to what you suggested and added a message box to confirm the position and although it displays the correct position it still doesn't put the cursor at the end of the text.

Here is my amended code

Code:
Private Sub Task_update_DblClick(Cancel As Integer)

        If Len(Me.[Task update] & vblnullstring) = 0 Then
                Me.[Task update].Value = Format(Now(), "dd-mm-yy") & ": "
        Else
                Me.[Task update].Value = Me.[Task update].Value & vbCrLf & Format(Now(), "dd-mm-yy") & ": "
        End If

'    Me.[Task update].SelStart = Me.[Task update].SelStart + Me.[Task update].SelLength
        
        MsgBox Me.[Task update].SelStart + Me.[Task update].SelLength ' just for testing position

        Me.[Task update].SelStart = Me.[Task update].SelLength


End Sub

I had a read of your suggested post but don't think it will help me. Thanks anyway.
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
I have managed to crack it on the live version and it was all down to your help Colin. I changed all the references as suggested but the last line of code I had to reference it with the underscore. Very odd this one but at least it is now working.

Here is my final code. I cant thank you enough with pointing me in the right direction. Thanks so much for helping a damsel in distress yet again. :D

Code:
Private Sub Task_update_DblClick(Cancel As Integer)

        If Len(Me.[Task update] & vblnullstring) = 0 Then
                Me.[Task update].Value = Format(Now(), "dd-mm-yy") & ": "
        Else
                Me.[Task update].Value = Me.[Task update].Value & vbCrLf & Format(Now(), "dd-mm-yy") & ": "
        End If

        Me.[Task update].SelStart = Me.[Task update].SelStart + Me.[Task update].SelLength
                
        Me!Task_update.SelStart = Me!Task_update.SelLength

End Sub
 

Snowflake68

Registered User.
Local time
Today, 17:01
Joined
May 28, 2014
Messages
452
I stand corrected. It is NOT working in the live application. I now get the same error again so back to the drawing board I'm afraid.
 

isladogs

MVP / VIP
Local time
Today, 17:01
Joined
Jan 14, 2017
Messages
18,227
In the code below
1. REMOVE the 2 lines in RED.
2. ADD the line in BLUE

Code:
Private Sub Task_update_DblClick(Cancel As Integer)

        If Len(Me.[Task update] & vblnullstring) = 0 Then
                Me.[Task update].Value = Format(Now(), "dd-mm-yy") & ": "
        Else
                Me.[Task update].Value = Me.[Task update].Value & vbCrLf & Format(Now(), "dd-mm-yy") & ": "
        End If

        [COLOR="Red"][B]Me.[Task update].SelStart = Me.[Task update].SelStart + Me.[Task update].SelLength[/B][/COLOR] ''<<REMOVE
                
        [COLOR="red"]Me!Task_update.SelStart = Me!Task_update.SelLength[/COLOR] '<<REMOVE

       [B][COLOR="Blue"]Me.[Task update].SelStart = Me.[Task update].SelLength[/COLOR][/B] '<<ADD THIS

End Sub
[/QUOTE]

And if that still fails ....

1. Add the Microsoft Forms VBA reference library - c:\windows\system32\fm20.dll

2. REPLACE ALL THE ABOVE CODE WITH THIS:

Code:
Private Sub Task_update_DblClick(Cancel As Integer)

   Dim clipboard As MSForms.DataObject
   Set clipboard = New MSForms.DataObject
   clipboard.SetText Me.[Task Update]  
    
End Sub
 

static

Registered User.
Local time
Today, 17:01
Joined
Nov 2, 2015
Messages
823
Access doesn't have a proper double click event. It times two single clicks, so it doesn't always work the way you want.

Code:
Dim goend As Boolean

Private Sub Task_Update_DblClick(Cancel As Integer)
    goend = True
End Sub

Private Sub Task_Update_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If goend Then
        Task_Update.SelLength = 0
        Task_Update.SelStart = Len(Task_Update)
        goend = False
    End If
End Sub
 

Users who are viewing this thread

Top Bottom