Passing KeyCode into function from Form Properties

GK in the UK

Registered User.
Local time
Today, 17:04
Joined
Dec 20, 2017
Messages
281
I have this code repeated for a number of bound controls (with the respective control names obviously). It copies control data from a datasheet into the equivalent control for a new record.

Code:
Private Sub tlDescription1_KeyDown(KeyCode As Integer, Shift As Integer)
    ' copy datasheet line detail to the new line
    ' whichever line is highlighted in the datasheet,
    ' copy the equivalent field to the new transaction line
    If KeyCode = CopyLineKey And bGotCopies Then
        tlDescription1 = frmDsheet.tlDescription1
        KeyCode = vbKeyReturn                   ' then simulate the return key
    End If

End Sub
Is there a way to build this into a function? I made a brief start but whilst I can get the name of the current control, I don't know if there is a way to pass the KeyCode into the function (from the control property event property sheet). Also, the line frmDsheet.strActiveCtl doesn't seem to be a valid reference, even though the explicit code in my working routine works.

So I'm looking for a way to call the function from the property sheet event tab, something like this:
Code:
= KeyDown (KeyCode, text as textbox)
Code:
' experimental code follows:

Private Function KeyDown(KeyCode As Integer, text As TextBox)
    ' usage:  =KeyDown (KeyCode, tlDescription1)
    ' copy datasheet line detail to the new line
    
    Dim strActiveCtl As String
    strActiveCtl = Screen.ActiveControl.Name

    ' code to fetch KeyCode from the event, and pass it into this function

    If KeyCode = CopyLineKey And bGotCopies Then
        text = frmDsheet.strActiveCtl            ' the underlying datasheet that we want to copy from
        KeyCode = vbKeyReturn                   ' then simulate the return key
    End If
End Function
I get an error 'The object doesn't contain the automation object 'KeyCode'

Edit: KeyDown probably not a good name for the function so I renamed it KeyDownTest but I get the same error
 
Last edited:
Create a class.
 
Thanks, arnelgp. I'm still working my way to the "o" in novice but maybe I need to shift my skills up a gear and understand classes.

In the meantime I find that instead of having an event procedure for every control, I can create a single event handler for the form and use Screen.ActiveControl.Name in there. Here's my code which replaces a sub for each control:

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    ' as long as form KeyPreview is set to on, the form receives key events first
    Dim strActiveCtl As String

    If KeyCode = CopyLineKey And bGotCopies Then
        ' we're at this control in a new record
        strActiveCtl = Screen.ActiveControl.Name
        ' copy the field data from the equivalent
        ' control, on the currently highlighted
        ' datasheet line
        Screen.ActiveControl = frmDsheet(strActiveCtl)
        ' now simulate the return key to go to next field
        KeyCode = vbKeyReturn
    End If

End Sub
 

Users who are viewing this thread

Back
Top Bottom