本文介绍了如何通过MS-Access VBA中的TextBox.OnKeyUp属性传递KeyCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MS Access中,我使用VBA为多个TextBox设置事件处理函数:

In MS Access, I use VBA to set an event handler function for multiple TextBoxes:

txtMyTextBox.OnKeyUp = "=myEventHandlerFunction()"

到目前为止,这很好.但是我想传递已按下的键的KeyCode.当我为文本框手动创建常规" KeyUp事件时,它会自动提供 KeyCode Integer Shift As Integer .我期望这些也可以与OnKeyUp属性一起使用,就像这样:

So far, this works fine. However I want to pass the KeyCode of the key that has been pressed. When I create "usual" KeyUp events manually for a TextBox, it automatically provides KeyCode As Integer and Shift As Integer. I was expecting that these could be used with the OnKeyUp property as well, like this:

txtMyTextBox.OnKeyUp = "=myEventHandlerFunction(KeyCode)"

当然,我更新了myEventHandlerFunction,以便它需要此参数.但是,触发事件时,Access给我一个错误,说该表达式导致错误,因为该对象不包含自动化对象 KeyCode .

Of course I updated myEventHandlerFunction so that it expects this argument. However, when the event is triggered, Access gives me an error, saying that the expression caused an error because the object doesn't contain the automation object KeyCode.

是否有可能使用OnKeyUp属性为myEventHandlerFunction提供KeyCode?我使用的是错误的格式还是错误的参数名称(如果使用的话,我使用德语的Access)?

Is there any possibility how I can provide myEventHandlerFunction with the KeyCode using the OnKeyUp property? Am I using the wrong format or even the wrong parameter name (I'm using Access in German language, if that matters)?

关于TextBox.OnKeyUp属性(访问)不提供与此有关的任何信息.

The MSDN documentation on the TextBox.OnKeyUp Property (Access) doesn't provide any information about this.

我知道我可以为每个TextBox手动设置KeyUp事件,并从那里使用KeyCode参数调用myEventHandlerFunction.这不是我想要的,因为我想以编程方式设置事件处理,并且据我所知,这需要使用OnKeyUp属性.

I know I could manually set up the KeyUp event for every TextBox and call myEventHandlerFunction from there with the KeyCode argument. This is not what I want since I want to set the event handling programatically, and to my understanding this requires using the OnKeyUp property.

推荐答案

问题是您处理事件的方式.您不应将程序处理程序设置为文本表达式,而应使用类和OOP为文本框设置事件处理程序.

The problem is the way you're handling events. You shouldn't set program handlers as text expressions, but instead use classes and OOP to set event handlers for your text boxes.

示例:

使用以下类:

clsTextboxHandler

Public textboxesHandler As clsTextboxesHandler
Public WithEvents txt As Access.Textbox

Private Sub txt_KeyUp(KeyCode As Integer, Shift As Integer)
    textboxesHandler.HandleKeyUp txt, KeyCode, Shift
End Sub

clsTextboxesHandler

Private TextboxHandlers As Collection

Private Sub Class_Initialize()
    Set TextboxHandlers = New Collection
End Sub

Public Sub LoadAllTextboxes(ByRef TheForm As Access.Form)
    Dim ctl As Control
    For Each ctl In TheForm.Controls
        If ctl.ControlType = acTextbox Then 'Add additional criteria to only handle certain textboxes
            LoadTextbox ctl                
        End If
    Next ctl
End Sub

Public Sub LoadTextbox(txt As Access.Textbox)
    Dim TextboxHandler As New clsTextboxHandler
    Set TextboxHandler.txt= txt
    Set TextboxHandler.textboxesHandler = Me
    txt.OnKeyUp = "[Event Procedure]"
    TextboxHandlers.Add TextboxHandler
End Sub

Public Sub HandleKeyUp(txt As Access.Textbox, KeyCode As Integer, Shift As Integer)
    'Handle your KeyUp for multiple textboxes here. You can use the textbox object, keycode, etc.
End Sub

在表格上:

Private TextboxesHandler As clsTextboxesHandler
Public Sub Form_Load()
    Set TextboxesHandler = New clsTextboxesHandler
    TextboxesHandler.LoadAllTextboxes Me
End Sub

这具有许多优点,例如能够在表单模块中为某些控件使用其他处理程序,并能够在您的处理程序中引用调用该事件的控件.

This has numerous advantages, such as being able to use additional handlers in the form module for certain controls, and being able to refer to the control calling the event in your handler.

这篇关于如何通过MS-Access VBA中的TextBox.OnKeyUp属性传递KeyCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:01