Select text in text box using MS Access VBA

In MS Access, Text boxes on a form or report is used for input or display data. Textbox which are used for display data and has record source is called a bound textbox. Bound textbox is bound to data in a field. Textbox can be unbound which is used to take input or for display result. Textbox has different properties, methods and events also.

This article is about to enable key press event "Ctrl+A" on textbox field. On the textbox field of MS Access Form or report we can perform cut, copy and paste by press keys on keyboard or using mouse. We can also able to select all the text on textbox using mouse, but as we press "CTRL+ A" the focus is lost. We are not able to use this key combination. In this article we are enable this key combination for "select all" the text on text field. If we want to enable "Ctrl+A" on textboxes then for each textbox we have to set the focus. But write code for each textbox is not a good way. In this article we are write code on key press event of Form. So this is for all the textboxes on the Form. To implement this we have to insert textbox on Form as shown in Fig 1.1.

MS Access VBA select all text in textbox. Fig-1.1


On key press event of Form we have to write code. After write VBA code, we have to test it. Now click on any textbox and press "Ctrl+A". It selects the text of textbox. In VBA code we are check the ASCII value of "Ctrl+A" i.e. is 1. We can enable other keys also if we know their ASCII values. So on Form key press event we are check the ASCII value to enable the Select all functionality on Textbox.

VBA code on Form Key Press event:

Option Compare Database
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim ctlCurrentControl As Control
If KeyAscii = 1 Then
Set ctlCurrentControl = Screen.PreviousControl
End If
End Sub


It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. ERP Makers will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.