How to pass KeyCode via TextBox.OnKeyUp property in MS-Access VBA

戏子无情 提交于 2019-12-02 02:53:57

问题


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

txtMyTextBox.OnKeyUp = "=myEventHandlerFunction()"

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)"

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.

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)?

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

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.


回答1:


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.

Example:

Use the following classes:

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

And on the form:

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.



来源:https://stackoverflow.com/questions/49789635/how-to-pass-keycode-via-textbox-onkeyup-property-in-ms-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!