vba 一定空闲时间后自动关闭表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23948836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:19:08  来源:igfitidea点击:

Automatically Close Form After Certain Idle Time

vbams-accesstimeraccess-vbams-access-2010

提问by jake

I am having a little bit problem on my access form.

我的访问表单有一点问题。

I have 2 forms:

我有两种形式:

  • menu-form
  • input-form
  • 菜单形式
  • 输入形式

After input-form has been idle for one minute, I want to close it and go back to the menu-form.

输入表单闲置一分钟后,我想关闭它并返回菜单表单。

This is my code that isn't working.

这是我的代码不起作用。

Public ExpireTime    As Date  'expiration time-date value

Sub ResetExpiration()             

    If ExpireTime <> 0 And ExpireTime > Now Then
        Application.OnTime ExpireTime, "FormExpire", schedule:=False
    End If

    ExpireTime = Now + 1 / 1440#    

    Application.OnTime ExpireTime, "FormExpire", schedule:=True

End Sub

I also created a macro with this in it.

我还用它创建了一个宏。

Sub FormExpire()
   Unload input-form
End Sub

回答by Patrick Honorez

You need to set the form.Timerto 60000 (that is 1 minute) then use on OnTimerevent to check if some properties have changed. Below I quickly wrote something to give you an idea, but it is not complete nor tested.

您需要将 设置form.Timer为 60000(即 1 分钟),然后使用 onOnTimer事件检查某些属性是否已更改。下面我快速写了一些东西给你一个想法,但它并不完整,也没有经过测试。

Option Compare Database
Option Explicit

Dim isIdle As Boolean


Private Sub Form_LostFocus()
    'you can use this event also
End Sub


Private Sub Form_Timer()
    Dim ctlName As String, wasDirty As Boolean
    If ctlName = vbNullString Then ctlName = Me.ActiveControl.Name
    If Me.ActiveControl <> ctlName Then isIdle = False
    If wasDirty <> Me.Dirty Then isIdle = False
    'more checks....
    If isIdle Then DoCmd.Close acForm, Me.Name
End Sub

回答by RubberDuck

You've got a couple of potential issues here. First and foremost, that's not how you close a form.

你在这里有几个潜在的问题。首先,这不是关闭表单的方式。

This:

这个:

Sub FormExpire()
   Unload input-form
End Sub

Should look something more like this:

应该看起来更像这样:

Sub FormExpire()
    DoCmd.Close acform, Me.Name
    DoCmd.OpenForm "menu-form"
End Sub

Your second issue is that I don't believe the Application.Ontimemethod is available from Access. MSDNsays it's an Excel function, and I can't find it in the Access Documentation. I'd personally be interested in seeing you make your method work with the Access form Timer Event.

您的第二个问题是我不相信该Application.Ontime方法可从 Access 获得。MSDN说它是一个 Excel 函数,我在Access Documentation 中找不到它。我个人很想看到您使您的方法与Access 表单 Timer Event 一起工作

I'm not sure it matters, but I think this is what you were trying to do.

我不确定这是否重要,但我认为这就是你想要做的。

Public ExpireTime    As Date  'expiration time-date value

Private Sub InputForm_Timer()             

    If ExpireTime <> 0 And ExpireTime > Now Then
        Call FormExpired
    Else
        ' reset timer
        ExpireTime = Now + 1 / 1440#             
    End If

End Sub

I'm not sure this method would work without also including @iDevelop's answer. I just wanted to help clarify where you were going wrong.

我不确定如果不包括@iDevelop's answer ,这种方法是否有效。我只是想帮助澄清你哪里出错了。