Solved Set the cursor position in a textbox using VBA (1 Viewer)

zelarra821

Registered User.
Local time
Today, 09:37
Joined
Jan 14, 2019
Messages
813
I am creating a class module to manage mouse movements in a textbox and the ribbons that should appear in rich texts.

I have some questions:

1. How can I use VBA to set the option to place the cursor at the end of the text that can be set in the Access options (Client Settings)? This way, I wouldn't have to worry about the configuration that each user had.

With this, the problem of selecting a part of the text with the mouse and placing the cursor there is solved, in addition to the fact that, upon entering the field, it would go directly to the end.

2. How do I detect which part of the text in the textbox the user has clicked on when there is text selected and they want to undo the selection?

This would allow me to solve the problem of it not letting me click on a specific part of the text that I just selected.

3. If I delete the text and do not exit the field (to save the changes), how do I detect that change without needing to exit the field?
 

Attachments

  • Database.accdb
    1.1 MB · Views: 66

CJ_London

Super Moderator
Staff member
Local time
Today, 08:37
Joined
Feb 19, 2013
Messages
16,612
1. No such setting as far as I know but you could try changing the read direction property for the control tho’ I suspect this will introduce other issues

2 don’t understand- if the user has selected some text, then clicks again, the selection is lost

3 before losing focus, compare the control’s text and value properties

you might want to review the selstart, sellength and selpos properties
 

zelarra821

Registered User.
Local time
Today, 09:37
Joined
Jan 14, 2019
Messages
813
No such setting as far as I know but you could try changing the read direction property for the control tho’ I suspect this will introduce other issues
Can you tell me how to do it please? Thank you so much.

don’t understand- if the user has selected some text, then clicks again, the selection is lost

See Point 2 (Video)

As you can see in the video, and then check in situ with the database that I attached previously, when I select text, and then try to click on some part of it, it does not undo the selection. Hence it asks how to know which position in the text the user has clicked on.

before losing focus, compare the control’s text and value properties

See Point 3 (Video)

As I explain in the video, I have a code to paste plain text that replaces Control + V, and that also has a button on the custom ribbon that I created.

This code determines the position where the user is copying the text and inserts the plain text at that position. However, if the user selects the text, deletes it, and then pastes, the position is 0, but I am left with the text the user deleted.

For that reason, I'm asking how to determine that the text has been deleted without having to exit the textbox.

Thanks!
 

Attachments

  • Point 2.zip
    4.9 MB · Views: 80
  • Point 3.zip
    2.4 MB · Views: 70

CJ_London

Super Moderator
Staff member
Local time
Today, 08:37
Joined
Feb 19, 2013
Messages
16,612
Can you tell me how to do it please?
1693699423316.png

however just tested - and does not work for what you want. But in the control gotfocus event put

myControl.SelLength = Len(myControl)

seems it gets you to the last line

point2 - think that is because of the richtext formatting options, change to plaintext and not a problem - but then a problem for point 1

point 3 - already answered, but would modify to

plaintext(mycontrol.text)<plaintext(mycontrol) then 'implies something has been deleted

Alternatively, might be you can use the keydown or keyup event to detect the del key has been used

In your example, you do not seem to be using richtext codes not available for plain text (such as colour/bold etc) so question why you are using richtext

@isladogs has done quite a bit of work on richtext fields, including his name in the post means he will be notified. He may be able to suggest something
 

Edgar_

Active member
Local time
Today, 02:37
Joined
Jul 8, 2023
Messages
430
How can I use VBA to set the option to place the cursor at the end of the text that can be set in the Access options (Client Settings)? This way, I wouldn't have to worry about the configuration that each user had.
Application.SetOption "behavior entering field", 2

2. How do I detect which part of the text in the textbox the user has clicked on when there is text selected and they want to undo the selection?

This would allow me to solve the problem of it not letting me click on a specific part of the text that I just selected.
If what you're referring to can be reproduced by doing this:
Enter rich textbox > format something > undo > undo again (all the text is selected and can't be deselected)

The culprit appears to be at Private Sub mText_Click(), if I comment it, the behavior stops. What is it supposed to be for?
 
Last edited:

zelarra821

Registered User.
Local time
Today, 09:37
Joined
Jan 14, 2019
Messages
813
All done.

I leave point 1 with a procedure to place the cursor at the end of the text and if the field is null, so that there is no error, I put it at the beginning.

I discovered point 2 was because it had the OnClick event, which was distorting its operation. It was deleted and this point was also resolved.

Finally, I have also solved point 3 with the proposed code.

Thanks a lot.

All the best.
 

Users who are viewing this thread

Top Bottom