Do Until causes Access to stop responding

Foe

Registered User.
Local time
Today, 06:01
Joined
Aug 28, 2013
Messages
80
I'm using the following code:
Code:
Private Sub Form_Timer()
Dim LocalTime, StartWindow, EndWindow As Date
LocalTime = Format(TimeValue(Now()), "hh:mm")
StartWindow = #11:46:00 AM#
EndWindow = #11:48:00 AM#
Dim Response
Response = MsgBox("Run scheduled maintenance?" & vbCrLf & vbCrLf & _
                          "Selecting YES will momentarily disable the database." & vbCrLf & _
                          "Select NO if you have unsaved work.", vbYesNo, "Scheduled Maintenance Required")
If LocalTime > StartWindow And LocalTime < EndWindow Then
    Do Until Response = vbYes
        'scheduled maintenance prompt
        If Response = vbYes Then
            'set logout flag - logout flag is monitored by other front ends to prevent back end connections
            DoCmd.SetWarnings False
            DoCmd.OpenQuery "qryLogoutTrue"
            DoCmd.SetWarnings True
            'close frmHome - this disconnects front end from back end
            DoCmd.Close acForm, "frmHome"
            'display maintenance message
            Me.Visible = True
            'run the module to backup, compact and repair
            CompactDB ("tblHotword")
            'hide maintenance message
            Me.Visible = False
            'reset logout flag
            DoCmd.SetWarnings False
            DoCmd.OpenQuery "qryLogoutFalse"
            DoCmd.SetWarnings True
            'reopen frmHome
            DoCmd.OpenForm "frmHome"
        Else
            StartWindow = DateAdd("n", 3, StartWindow)
            EndWindow = DateAdd("n", 3, EndWindow)
        End If
    Loop
End If
End Sub

The intent is to run automated back end maintenance activities while giving the user the option to delay it if they're in the middle of something.

My logic is as follows:
If it's between these two times, ask the user if the maintenance can run.
If user says yes, run maintenance.
If user says no shift the two times to the right and ask again in a few minutes.

As it is written, when I chose no I get an hourglass icon and if I try to do anything else Windows tells me Access isn't responding. I'm guessing that Access is hanging since I've initiated the Do Until and then stalled it by telling it to wait 3 minutes before it goes to the next step. I suspect that if I waited it out, when the new Startwindow hit I would be asked about running the maintenance again.

My issue is that I need the database to remain responsive to whatever the user needs to do while I wait to ask about running maintenance again. Therefore, I don't think Do Until is my answer - at least not the way I've implemented it.

Anyone have any suggestions to accomplish my goal? The will ultimately be set on a 10 minute timer interval and start/end window will be 01:00 and 01:15 AM respectively. The times you see above are there for testing so I can see what happens.
 
I don't think you want the loop either. I'd use form level variables, so resetting the times should make the timer ask again after the set interval. Depending on your timer interval, I suspect you'll want to flag the process as done so it isn't done again within the time frame.
 
Got this one resolved. I did away with the Do Loop and added two labels to my frmMaintenance. The first label's caption is my "start window" for maintenance and the second label's caption is my "end window." Both labels are set to visible=false.

I make use of the labels with the following code:
Code:
Private Sub Form_Timer()
Dim LocalTime, StartWindow, EndWindow As Date
LocalTime = TimeValue(Now())
StartWindow = TimeValue(Me.lblStartWindow.Caption)
EndWindow = TimeValue(Me.lblEndWindow.Caption)
If LocalTime > StartWindow And LocalTime < EndWindow Then
    'scheduled maintenance prompt
    Dim Response
    Response = MsgBox("Run scheduled maintenance?" & vbCrLf & vbCrLf & _
                    "Selecting YES will momentarily disable the database." & vbCrLf & _
                    "Select NO if you have unsaved work.", vbYesNo, "Scheduled Maintenance Required")
    If Response = vbYes Then
        'set logout flag - logout flag is monitored by other front ends to prevent back end connections
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "qryLogoutTrue"
        DoCmd.SetWarnings True
        'close frmHome - this disconnects front end from back end
        DoCmd.Close acForm, "frmHome"
        'display maintenance message
        Me.Visible = True
        'run the module to backup, compact and repair
        CompactDB ("tblHotword")
        'hide maintenance message
        Me.Visible = False
        'reset logout flag
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "qryLogoutFalse"
        DoCmd.SetWarnings True
        'reopen frmHome
        DoCmd.OpenForm "frmHome"
        'reset times to default
        Me.lblStartWindow.Caption = "1:00:00 AM"
        Me.lblEndWindow.Caption = "1:05:00 AM"
    Else 'move maintenance window to the right 5 minutes
        Dim strStart, strEnd As String
        strStart = DateAdd("n", 5, StartWindow)
        strEnd = DateAdd("n", 5, EndWindow)
        Me.lblStartWindow.Caption = strStart
        Me.lblEndWindow.Caption = strEnd
    End If
End If
End Sub

If, when prompted for maintenance, the user selects No, I add 5 minutes to StartWindow and EndWindow. This happens each time they select No. When the user selects Yes, the maintenance happens and the labels that control the Start/End window are reset to their default values.

The timer interval is set to 300000 (5 minutes), thereby circumventing the need to flag whether maintenance has occurred or not, since the timer will only trigger once per window. I figure this also cuts down an unnecessary overhead from firing the timer too often since this is the only event tied to it. I'd have preferred a longer interval, but I didn't want to delay re-asking to run maintenance too long.
 
I am glad to see you found a work-around for your challenge, Foe.

Occasionally I have run into "Access not responding" nonsense. In such cases I have managed to work around it that the code was loop based and did not interact with the UI at all. At the end of the loop code before it re-runs the entire loop, I place a...

Code:
  'Keep UI responsive
  DoEvents
and that is "enough" to prevent the "Access not responding" nonsense.
 

Users who are viewing this thread

Back
Top Bottom