0

我有一个名为 frmReportMetrics 的 Access 表单,它作为显示表单打开。在打开时,它使用UserIsInGroup()来验证权限。如果用户不在指定的组中,则 Select Case - Case False 语句将关闭 frmReportMetrics 并打开 frmAccessDenied,这只是一个带有文本的停车标志图像,警告个人他们无权使用该应用程序。

'Set default values to form items
Private Sub Form_Open(Cancel As Integer)
'Check to assure user has privileges to run front-end
    Select Case UserIsInGroup("The Reports")
        Case False
            DoCmd.Close acForm, Me.Name, acSaveNo
            DoCmd.OpenForm "frmAccessDenied", acNormal, "", "", , acNormal
            Exit Sub
    End Select
    Me.lblTabGoToManagersReportsPage.Visible = False

'Only display the label if the user is a member of the security group
    Select Case UserIsInGroup("The Reports - Managers")
        Case True
            Me.lblTabGoToManagersReportsPage.Visible = True
    End Select
End Sub

然后我希望应用程序在显示倒计时 5 秒后自动关闭。所以我 在 frmAccessDenied 中使用了pause()

Private Sub Form_Activate()

    Me.lblClosingIn.Caption = "This form will close in 5 seconds"
    Pause (1)
    Me.lblClosingIn.Caption = "This form will close in 4 seconds"
    Pause (1)
    Me.lblClosingIn.Caption = "This form will close in 3 seconds"
    Pause (1)
    Me.lblClosingIn.Caption = "This form will close in 2 seconds"
    Pause (1)
    Me.lblClosingIn.Caption = "This form will close in 1 seconds"
    Pause (1)
    Application.Quit

End Sub

我敢肯定它可以更短...

问题是在测试时,我将自己从 AD 安全组中删除并打开 Access 前端,frmAccessDenied 表单没有按预期弹出,但应用程序确实在 5 秒内退出。我也从未见过 frmReportMetrics。我在 frmAccessDenied 中尝试了 _Load、_Open、_Activate 和 _Current,但它们都不允许出现 frmAccessDenied。_GoftFocus 工作并且 frmAccessDenied 出现,我看到停止标志和警报,但是倒计时没有继续,应用程序没有在 5 秒内退出。

当我逐步完成 frmAccessDenied 时,我可以在任何时候重置并且 frmAccessDenied 出现,我看到带有警报的停止标志以及底部的适当 Me.lblClosingIn.Caption 并且如果我一直通过应用程序退出。 在此处输入图像描述

我是否在某处错过了诸如 Exit Sub 之类的东西?或者我应该使用什么事件过程?

UserIsInGroup() 和 pause() 都按预期工作,分别感谢 @Nigel Heffernan 和 @Steve Mallory

TIA,蒂姆

PS @Erik AI 全部添加了 DoEvents,但仍然没有绘制 frmAccessDenied。

Private Sub Form_Activate()
    DoEvents

    Dim I As Integer
    Dim sFirstPart As String
    Dim sSecondPart As String
    Dim sCompleteSentence As String

    sFirstPart = "This form will close in "
    sSecondPart = " seconds!"

    For I = 5 To 2 Step -1
        sCompleteSentence = sFirstPart & I & sSecondPart
        DoEvents
        Me.lblClosingIn.Caption = sCompleteSentence
        Pause (1)
    Next
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 1 second!"
    Pause (1)
    'Application.Quit
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 5 seconds"
    Pause (1)
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 4 seconds"
    Pause (1)
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 3 seconds"
    Pause (1)
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 2 seconds"
    Pause (1)
    DoEvents
    Me.lblClosingIn.Caption = "This form will close in 1 second"
    Pause (1)
    'Application.Quit
End Sub

我尝试了 For Next 但这也无济于事。

当我调试并运行 _Open、_Load、_Activate 和 _Current 时,每个都有断点,frmAccessDenied 永远不会变得可见。我什至尝试过自由应用 DoEvents!

Option Compare Database
Option Explicit

Private Sub Form_Activate()
    DoEvents
    Me.txtMessage.Value = "This application will close in 3 seconds!"
End Sub

Private Sub Form_Current()
    DoEvents
    Me.txtMessage.Value = "This application will close in 2 seconds!"
End Sub

Private Sub Form_Load()
    DoEvents
    Me.txtMessage.Value = "This application will close in 4 seconds!"
End Sub

Private Sub Form_Open(Cancel As Integer)
    DoEvents
    Me.txtMessage.Value = "This application will close in 5 seconds!"
End Sub

当表单最终弹出时,其值为“此应用程序将在 2 秒内关闭!”

我错过了什么?

瞧!@汤姆罗宾逊!这是了解 Access 内部工作原理的一个很好的小块。表单虽然已创建,但在 Access 使其可见或使其可见之前是不可见的。我继续在 _Open 上显示表单,在 _Load 中显示倒计时。按规格工作!

Option Compare Database
Option Explicit
Private Sub Form_Load()
Dim i As Integer
Dim txtFirstPart As String
Dim txtSecondPart As String
txtFirstPart = "This application will close in "
txtSecondPart = " seconds!"
For i = 5 To 2 Step -1
    Me.txtMessage.Value = txtFirstPart & i & txtSecondPart
    Pause (1)
Next
Me.txtMessage.Value = "This application will close in 1 second!"
Pause (1)
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub Form_Open(Cancel As Integer)
    Me.Visible = True
End Sub
4

2 回答 2

2

因为您的子程序同步运行,所以它不会给 Access 时间来显示表单,因为您的代码在表单绘制之前运行。

要解决此问题,请使用 启动您的子程序DoEvents(),并在更改表单内容后重复此操作。

或者,您可以使用_Timer倒计时并关闭表单,而不会使应用程序无响应。

于 2020-01-03T19:43:32.217 回答
1

添加Me.Visible = TrueForm_Activate.

默认情况下,在完成各种表单事件之前,新打开的表单不会变得可见。这样用户就不会看到分散注意力的启动活动。

于 2020-01-06T21:42:53.157 回答