Do you know what happens if you are in an Access text box and press Ctrl-A?
You might expect all the text in it to be selected like a text box on a web page or in a Word document. But it doesn’t. If you are in a text box on a form in the normal single form view it appears to do absolutely nothing.
However if you are in datasheet view, it will select all the fields and all the records. Here’s a nice list of shortcuts (including Ctrl-A) collected by Colin Ridington on his web site: https://isladogs.co.uk/keyboard-shortcuts/index.html
So what are the options for selecting all the text in a text box?
One trick I saw on a forum post was to press TAB then Shift-TAB. This moves to the next control then back to the original control. When tabbing between controls the default behavior is for Access to select all the text.
Another option would be to position your cursor at the beginning or end of the text and use a combination of arrow keys, home and/or end while holding down shift.
Yet another option would be to click once on the field so the cursor is placed there with nothing selected and then triple click to select all the text in the box.
But what if you want to change the behavior of Access with VBA code?
I think the general idea would be:
- Make sure your form is set to see keystrokes.
- Use event handling on jthe form to capture keyboard strokes.
- Detect whether Ctrl-A is being pressed.
- If so, detect whether you are on a text box control.
- If so, abort the Ctrl-A keystroke, set the cursor to the beginning of the text box and select all of the text in the text box using VBA.
That’s pretty much it, but wouldn’t it be great to have that in a class module? You could load the class module in the Open event of your form, run some code to initialize the class which would set the correct parameters on the form and listen for the form’s keyboard stroke event that you can cancel.
Here would be a good place to start. Much of the code is written in this StackOverflow article: vba – How to use Ctrl+A in a MS-Access textbox in order to select all the text – standard module option – Stack Overflow
Let me know if you write a class to do this. If I end up doing that I’ll let you know.
Ctrl+A isn’t intended for selecting the text in a single text box.
If you are in a single or continuous form and press Ctrl+A, all the data is selected (all fields in all records). However, unlike datasheets, the only visual clue that has happened is that the record selectors are highlighted.
You can easily test that the data has been selected by then pressing Ctrl+C & pasting into e.g. a text editor
Unable to edit my previous comment to add more info.
To select all text in a textbox, Use a combination of SelStart & SelLength
To copy all text to another textbox on a button click event use something like:
Private Sub cmdCopy_Click()
Me.Text0.SetFocus
Me.Text0.SelStart = 0
Me.Text0.SelLength = Len(Me.Text0)
DoCmd.RunCommand acCmdCopy
Me.Text3.SetFocus
DoCmd.RunCommand acCmdPaste
End Sub
Thanks for your valuable insights and instructions Colin!
Your’re welcome. One more thing: TAB followed by SHIFT+TAB will only work if the client settings behaviour on entering a field is set to Select entire field.
I’ve just published a short article on this topic: https://isladogs.co.uk/select-all-text