【发布时间】:2019-12-25 17:03:11
【问题描述】:
我找到了一个solution,它允许我在 Access 文本框中使用 Ctrl+A 组合来选择其中的所有文本。
此解决方案需要:
- 将
Form.KeyPreview属性设置为True - 将以下代码添加到
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