简体   繁体   中英

MS Access get sender textbox on KeyDown event in vba

I have KeyDonw event handler on my form in MS Access 2007, i use it instead masks, because they work not like i need.

Here my code:

Private Sub date_rogd_s_d_KeyDown(KeyCode As Integer, Shift As Integer)
    If ([Forms]![aForm].Form.date_rogd_s_d.SelLength = 2) Then
        [Forms]![aForm].Form.date_rogd_s_d.Text = ""
    End If

    If (val([Forms]![aForm].Form.date_rogd_s_d.Text) > 31) Then
        Select Case KeyCode
           Case vbKeyDelete, vbKeyBack, vbKeyReturn
                X = Y
                Exit Sub
        Case Else
            KeyCode = 0
            Exit Sub
        End Select
    End If


    If (Len([Forms]![aForm].Form.date_rogd_s_d.Text) < 2) Then
        Select Case KeyCode
            Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
            Case 96, 97, 98, 99, 100, 101, 102, 103, 104, 105
           Case vbKeyDelete, vbKeyBack, vbKeyReturn
            X = Y
        Case Else
            KeyCode = 0
        End Select
    Else
        Select Case KeyCode
           Case vbKeyDelete, vbKeyBack, vbKeyReturn
                X = Y
                Exit Sub
        End Select
        [Forms]![aForm].Form.date_rogd_s_m.SetFocus
    End If
End Sub

this code works just fine, but i have more 5 same fields i need to switch between.

Any way to get sender of event, i mean textbox object inside key pressed, i want to create universal function for all textboxes and i dot wont to copy-paste this code.

I solve this problem with this function:

Private Sub onKeyDownForDateFields(KeyCode As Integer, Shift As Integer, Sender As Object, NextObject As Object, count As Integer, maxValue As Integer, Is_submit As Boolean)
    If (Sender.SelLength = count) Then
        Sender.Text = ""
    End If

    If (val(Sender.Text) > maxValue) Then
        Select Case KeyCode
           Case vbKeyDelete, vbKeyBack
                X = Y
                Exit Sub
        Case Else
            KeyCode = 0
            Exit Sub
        End Select
    End If


    If (Len(Sender.Text) < count) Then
        Select Case KeyCode
            Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
            Case 96, 97, 98, 99, 100, 101, 102, 103, 104, 105
           Case vbKeyDelete, vbKeyBack, vbKeyReturn
            X = Y
        Case Else
            KeyCode = 0
        End Select
    Else
        Select Case KeyCode
           Case vbKeyDelete, vbKeyBack
                X = Y
                Exit Sub
            Case vbKeyReturn
                Êíîïêà48_Click
                Exit Sub
        End Select
        If (Is_submit = True) Then
            Êíîïêà48_Click
        Else
            NextObject.SetFocus
        End If
    End If
End Sub

Where Sender is current TextBox and NextObject object i need to focus next.

It also test if max number symbols entered in text box and if, for example it have not reach max allowed number, 31 for days for example.

Now if i need to handle event i do it like this:

Private Sub date_rogd_s_d_KeyDown(KeyCode As Integer, Shift As Integer)

  onKeyDownForDateFields KeyCode, Shift, [Forms]![aForm].Form.date_rogd_s_d, [Forms]![aForm].Form.date_rogd_s_M, 2, 31, False  

End Sub

Rename your procedure to this, keep all the rest of the code:

Private Sub On_KeyDown(KeyCode As Integer, Shift As Integer)

then in all your controls/textboxes, pass the parameters to this procedure: for example, the event you have above would simply pass the keycode,shift as reference to the same sub:

Private Sub date_rogd_s_d_KeyDown(KeyCode As Integer, Shift As Integer)
 Call On_KeyDown(KeyCode, Shift)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM