【问题标题】:How to use Ctrl+A in a MS-Access textbox in order to select all the text - standard module option如何在 MS-Access 文本框中使用 Ctrl+A 以选择所有文本 - 标准模块选项
【发布时间】:2019-12-25 17:03:11
【问题描述】:

我找到了一个solution,它允许我在 Access 文本框中使用 Ctrl+A 组合来选择其中的所有文本。

此解决方案需要:

  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 关键字。

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

【问题讨论】:

  • 如果您打算这样做,那么也将文本框控件作为参数包含在模块中的函数中,然后引用该变量而不是您现在的方式。跨度>
  • 你能告诉我怎么做吗?!
  • Public Sub CtrlA(txt As TextBox, KeyCode As Integer, Shift As Integer)
  • 我尝试使用您的文本框参数,但现在它告诉我“ByRef 参数类型不匹配”关于 Call CtrlA(KeyCode, Shift) 行上的键码..

标签: vba ms-access textbox keydown ctrl


【解决方案1】:

按照您的代码示例,这就是您想要的:

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

参数KeyCode 必须定义为ByRef 才能将值0 返回给调用过程。

从表单中这样调用它:

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

在一个模块中:

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 的建议(感谢他)传递表单本身,您可以这样做:

从表单中这样调用它:

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

在一个模块中:

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

【讨论】:

  • 要使这个工作与多个控件一起工作,最好将表单(Me)作为参数传递,并在模块中使用F.ActiveControl。嗯,或者使用Screen.ActiveControl
猜你喜欢
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2020-09-11
  • 1970-01-01
相关资源
最近更新 更多