Access表单不显示但过程运行。

huangapple go评论77阅读模式
英文:

Access form doesn't appear but procedure runs

问题

I have an Access form titled frmReportMetrics that opens as the Display Form. On Open it uses UserIsInGroup() to verify authority. If the user is not in the specified group then a Select Case - Case False statement closes frmReportMetrics and opens frmAccessDenied which is just a stop sign image with text alerting the individual that they do not have permission to use the application.

'设置表单项的默认值
Private Sub Form_Open(Cancel As Integer)
'检查用户是否具有运行前端的权限
    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

'只有用户是安全组的成员才显示标签
    Select Case UserIsInGroup("The Reports - Managers")
        Case True
            Me.lblTabGoToManagersReportsPage.Visible = True
    End Select
End Sub

I then want the app to close automatically after 5 seconds displaying a countdown. So I used pause() in frmAccessDenied

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 second"
    Pause (1)
    Application.Quit

End Sub

I'm sure it could be shorter...

Trouble is when testing, and I remove myself from the AD security group and open the Access front-end, the frmAccessDenied form does not pop up as expected but the app does exit in 5 seconds. I don't ever see frmReportMetrics either. I've tried _Load, _Open, _Activate, and _Current in frmAccessDenied but none of them allow frmAccessDenied to appear. _GoftFocus works and frmAccessDenied appears and I see the stop sign and alerts but then the countdown does not proceed and the app does not quit in 5 seconds.

As I step through frmAccessDenied I can Reset at any point and frmAccessDenied appears and I see the stop sign with the alerts as well as the appropriate Me.lblClosingIn.Caption at the bottom and if I step all the way through the app quits.

Am I missing something like an Exit Sub somewehere? Or what Event Procedure should I use?

Both UserIsInGroup() and pause() work as expected, thanks @Nigel Heffernan and @Steve Mallory, respectively

TIA,
Tim

PS @Erik A I added DoEvents all over and still frmAccessDenied is not painted.

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

And I tried a For Next but that didn't help either.

As I debug and run through the _Open, _Load, _Activate, and _Current with breakpoints at each, frmAccessDenied never becomes visible. I even tried with liberal applications of 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

And when the form finally pops up it is with the value "This application will close in 2 seconds!"

What am I missing?

Voila! @Tom Robinson! That is a nice little nugget to understand the inner workings of Access. The form, though created is not visible until Access let's it be visible or until you make it visible. I went ahead and made the form visible on _Open and the countdown in _Load. Works to spec!

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)
End For
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
英文:

I have an Access form titled frmReportMetrics that opens as the Display Form. On Open it uses UserIsInGroup() to verify authority. If the user is not in the specified group then a Select Case - Case False statement closes frmReportMetrics and opens frmAccessDenied which is just a stop sign image with text alerting the individual that they do not have permission to use the application.

'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

I then want the app to close automatically after 5 seconds diplaying a countdown. So I used pause() in frmAccessDenied

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

I'm sure it could be shorter...

Trouble is when testing, and I remove myself from the AD security group and open the Access front-end, the frmAccessDenied form does not pop up as expected but the app does exit in 5 seconds. I don't ever see frmReportMetrics either. I've tried _Load, _Open, _Activate, and _Current in frmAccessDenied but none of them allow frmAccessDenied to appear. _GoftFocus works and frmAccessDenied appears and I see the stop sign and alerts but then the countdown does not proceed and the app does not quit in 5 seconds.

As I step through frmAccessDenied I can Reset at any point and frmAccessDenied appears and I see the stop sign with the alerts as well as the appropriate Me.lblClosingIn.Caption at the bottom and if I step all the way through the app quits.
Access表单不显示但过程运行。

Am I missing something like an Exit Sub somewehere? Or what Event Procedure should I use?

Both UserIsInGroup() and pause() work as expected, thanks @Nigel Heffernan and @Steve Mallory, respectively

TIA,
Tim

PS @Erik A I added DoEvents all over and still frmAccessDenied is not painted.

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

And I tried a For Next but that didn't help either.

As I debug and run through the _Open, _Load, _Activate, and _Current with breakpoints at each, frmAccessDenied never becomes visible. I even tried with liberal applications of 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

And when the form finally pops up it is with the value "This application will close in 2 seconds!"

What am I missing?

Voila! @Tom Robinson! That is a nice little nugget to understand the inner workings of Access. The form, though created is not visible until Access let's it be visible or until you make it visible. I went ahead and made the form visible on _Open and the countdown in _Load. Works to spec!

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

答案1

得分: 2

因为你的子程序运行是同步的,它不会给访问时间来显示表单,因为你的代码在表单被绘制之前运行。

为了解决这个问题,在你的子程序开始时使用DoEvents(),并在改变表单内容后重复使用它。

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

英文:

Because your sub runs synchronously, it does not give Access time to display the form, as your code runs before the form gets painted.

To work around this, start your sub with DoEvents(), and repeat it after changing the form contents.

Alternatively, you could use _Timer to count down and close the form without making the application unresponsive.

答案2

得分: 1

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

默认情况下,新打开的窗体直到各种窗体事件完成之前才会变为可见。这是为了让用户不会看到令人分心的启动活动。

英文:

Add Me.Visible = True as the first line of Form_Activate.

By default, a newly-opened form does not become visible until various form events have completed. This is so the user doesn't see distracting startup activities.

huangapple
  • 本文由 发表于 2020年1月4日 01:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定