如果按下某个键,则可以看到一个按钮

时间:2017-10-24 16:45:14

标签: vba button ms-access-2007 keypress

我正在尝试仅在按下键(示例控件)时才显示按钮。

我的代码:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then
Me.btn1.Visible = True
Else
Me.btn1.Visible = False
End If
End Sub

Private Sub Form_Load()
Me.btn1.Visible = False
End Sub

只有当按下CONTROL键并且用户点击btn1时,我才需要显示和激活按钮。

谢谢。

更新代码:

Option Compare Database
Option Explicit

Private Sub btnHide_Click()
    DoCmd.Close acForm, "frmDemo", acSaveYes
End Sub

Private Sub btnFake_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo Err_Handler
    If vbKeyControl = 17 Then
        Me.btnHide.Visible = True
        FormTimer
        Me.btnFake.SetFocus
        Me.btnHide.Visible = False
    Else
        Me.btnHide.Visible = False
    End If
Exit_This_Sub:
    Exit Sub
Err_Handler:
    If Err = 2467 Then '<== Form is closed.
        Resume Exit_This_Sub
    Else
        MsgBox "Error #: " & Err.Number & " " & Err.Description
    End If
Resume Exit_This_Sub
End Sub

Private Sub Form_Load()
    Me.btnHide.Visible = False
    Me.btnFake.SetFocus
End Sub

Sub FormTimer()
Dim PauseTime, Start, Finish, TotalTime    
    PauseTime = 0.1    ' Set duration.
    Start = Timer    ' Set start time.
        Do While Timer < Start + PauseTime
            DoEvents    ' Yield to other processes.
        Loop
End Sub

我测试它并且工作正常。简单的代码。 如果控制是按btnHide可见0.1秒。

有什么想法吗?

0 个答案:

没有答案