MS Access在vba中的KeyDown事件上获取发件人文本框

时间:2017-03-14 01:41:27

标签: forms vba access-vba ms-access-2007 access

我在MS Access 2007中的表单上有KeyDonw事件处理程序,我使用它代替掩码,因为它们不像我需要的那样工作。

这是我的代码:

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

这段代码工作正常,但我需要切换更多5个相同的字段。

任何方式获取事件的发件人,我的意思是按下键内的文本框对象,我想为所有文本框创建通用函数,我不会复制粘贴此代码。

2 个答案:

答案 0 :(得分:0)

我用这个函数解决了这个问题:

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

Sender是当前的TextBox和NextObject对象,我需要关注它。

它还会测试是否在文本框中输入了最大数字符号,例如是否未达到最大允许数量,例如31天。

现在,如果我需要处理事件,我会这样做:

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

答案 1 :(得分:-1)

将您的程序重命名为此,保留所有其余代码:

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

然后在所有控件/文本框中,将参数传递给此过程:例如,上面的事件只会传递键码,作为参考移位到同一个子码:

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