单击和双击事件(VBA)上的MS Access按钮

时间:2017-05-24 15:56:00

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

我在ms-access中的表单上有一个按钮,我在其中填充了“On Click”和“on Dbl Click”事件。我遇到了单击事件工作的问题,但双击不会,除非在第二次点击时,我一直按住它并在释放它之前将鼠标拖离按钮。此按钮的作用是根据2次不同的点击填充包含特定文本的文本框。我怎么能修复这个双击问题?按钮的代码如下。请注意,Task_ID_AfterUpdate()还可以通过在查询中查找Me.Task_ID来填充文本字段。

'single click
Private Sub getBtn_Click()
    btnUpdateHelper (1162)
End Sub
'double click
Private Sub getBtn_DblClick(cancel As Integer)
    btnUpdateHelper (1449)
End Sub
Private Sub btnUpdateHelper(Task As Variant)
    'helper method for buttons, only pass task id
    'Me.Task_ID is a textfield
    Me.Task_ID = Task
    Task_ID_AfterUpdate
End Sub
Private Sub Task_ID_AfterUpdate()
    Dim taskIdNum As String
    taskIdNum = Me.Task_ID
    'updates all fields to display the selected task
    Me.Area = DLookup("Area", "Query", "TaskID=" & taskIdNum)
    Me.Activity = DLookup("Activity", "Query", "TaskID=" & taskIdNum)
    Me.Description = DLookup("Description", "Query", "TaskID=" & taskIdNum)
    Me.Comments = DLookup("Comments", "Query", "TaskID=" & taskIdNum)
    Me.Task_Group = DLookup("[Task Group]", "Query", "TaskID=" & taskIdNum)
    Me.Mul = DLookup("Mul", "Query", "TaskID=" & taskIdNum)
    Me.Time = DLookup("Time", "Query", "TaskID=" & taskIdNum)
End Sub

1 个答案:

答案 0 :(得分:1)

ClickDblClick事件放在按钮上是IMO一个相当令人困惑的UI设计。更好的方法是使用右键单击或Shift +单击(或Alt或Ctrl)来执行备用功能。

Click事件不会捕获这些修饰符,只有MouseDownMouseUp。如果将操作放在MouseUp事件过程中,例如

,则感觉最自然
Private Sub cmdMultiClick_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim SelectedFunction As String

    If Button = acLeftButton Then
        If (Shift And acShiftMask) Then
            SelectedFunction = "Shift-Left click!"
        Else
            SelectedFunction = "Left click!"
        End If
    ElseIf Button = acRightButton Then
        SelectedFunction = "Right click!"
        ' Cancel the default right-click behavior (open context menu)
        DoCmd.CancelEvent
    End If

    MsgBox SelectedFunction, vbInformation

End Sub