如何在 MS-Access 文本框中使用 Ctrl+A 以 select 所有文本 - 标准模块选项

How to use Ctrl+A in a MS-Access textbox in order to select all the text - standard module option

我找到了一个 ,它允许我在 Access 文本框中使用 Ctrl+A 组合键,以便select里面的所有文字。

此解决方案需要:

  1. Form.KeyPreview属性设为True
  2. 将以下代码添加到 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

我试图将此代码放入这样的模块中:

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

为了像这样在任何地方调用它:

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

但标准模块中不允许使用 Me 关键字。

我怎样才能实现这个目标?

根据您的代码示例,这就是您想要的:

您必须将控件(此处命名为 text0)转发给您的程序。

必须定义参数 KeyCode ByRef 才能将 return 值 0 传递给调用过程。

Call it like this from in a form:

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

In a module:

Public Sub CtrlA(ByVal contextControl As Control, ByRef KeyCode As Integer, ByVal 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 contextControl Is TextBox Then
            With contextControl
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
        End If
    End If

ExitSub:
End Sub

按照 Andres 的建议(对他表示感谢)传递表单本身,您可以这样做:

Call it like this from in a form:

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

In a module:

Public Sub CtrlA(ByVal contextForm As Form, ByRef KeyCode As Integer, ByVal 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 contextForm.ActiveControl Is TextBox Then
            With contextForm.ActiveControl
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
        End If
    End If

ExitSub:
End Sub