访问表单未出现但程序运行

Access form doesn't appear but procedure runs

我有一个名为 frmReportMetrics 的访问表单,它作为显示表单打开。在打开时,它使用 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,但其中 none 允许出现 frmAccessDenied。 _GoftFocus 工作并且 frmAccessDenied 出现并且我看到停止标志和警报但是倒计时没有继续并且应用程序没有在 5 秒内退出。

当我单步执行 frmAccessDenied 时,我可以随时重置,frmAccessDenied 会出现,我会在底部看到带有警报的停止标志以及相应的 Me.lblClosingIn.Caption,如果我一直单步执行应用程序退出。

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

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

TIA, 蒂姆

PS @Erik A 我添加了 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

当表单最终弹出时,它的值为 "This application will close in 2 seconds!"

我错过了什么?

瞧! @汤姆罗宾逊!这是了解 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

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

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

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

添加Me.Visible = True作为Form_Activate的第一行。

默认情况下,新打开的表单只有在各种表单事件完成后才会显示。这样用户就不会看到分散注意力的启动活动。