(Solved) – How to use Ctrl+A in a textbox in order to select all the text – global option

  • by
(solved)-–-how-to-use-ctrl+a-in-a-textbox-in-order-to-select-all-the-text-–-global-option

I found this solution that will allow me to use Ctrl A combo in an Access textbox in order to select all the text inside it.

This solution needs:

  1. set the Form.KeyPreview property to True
  2. add this code to the Form.Keydown
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyA And Shift = acCtrlMask Then 'Catch Ctrl A
        KeyCode = 0 'Suppress normal effect
        On Error GoTo ExitSub 'ActiveControl causes a runtime error if none is active
        If TypeOf Me.ActiveControl Is TextBox Then
            With Me.ActiveControl
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
        End If
    End If
ExitSub:
End Sub

I tried to put this code in a module like this:

Public Sub CtrlA(KeyCode As Integer, Shift As Integer)
 If KeyCode = vbKeyA And Shift = acCtrlMask Then 'Catch Ctrl A
        KeyCode = 0 'Suppress normal effect
        On Error GoTo ExitSub 'ActiveControl causes a runtime error if none is active
        If TypeOf Me.ActiveControl Is TextBox Then
            With Me.ActiveControl
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
        End If
    End If
ExitSub:
End Sub

in order to call it wherever i want like this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 Call CtrlA(KeyCode, Shift)
End Sub

but the Me keyword isn’t allowed in a standard module..

How can I achive this goal?!

Leave a Reply

Your email address will not be published. Required fields are marked *