שתף באמצעות


delay or pause my VB code

Question

Tuesday, January 31, 2006 12:51 PM | 1 vote

I am having trouble with an Access form which runs some VB code behind it. The form accepts lots of user inputs and these are then grabbed by my VB code to calculate a result. This all works fine but does take some time for the calculation to complete due to the size of the datasets. 

Rather than the user having to stare at the same screen and wonder what is happening, I have another form which pops up to tell the user the calculations are in progress. The problem I am having is that this window will not display properly until after the calculation is completed. It seems as though Access cannot handle displayinging my window whilst the calculation is being caried out.

Is there any way to delay the calculation in VB so that my window will display correctly? The calculation can then take place after this. Here is my code:

Private Sub Calc_Click()
Dim ccombo(11) As Integer
Dim Total As Integer
DoCmd.OpenForm "calcsinprog"

I would like some kind of delay or pause here to allow the window to open properly before the calculations below.

ccombo(2) = Me.c2combo.Column(1, Me.c2combo.ListIndex)
ccombo(3) = Me.c2combo.Column(1, Me.c3combo.ListIndex)
ccombo(4) = Me.c2combo.Column(1, Me.c4combo.ListIndex)
ccombo(5) = Me.c2combo.Column(1, Me.c5combo.ListIndex)
ccombo(6) = Me.c2combo.Column(1, Me.c6combo.ListIndex)
ccombo(7) = Me.c2combo.Column(1, Me.c7combo.ListIndex)
ccombo(8) = Me.c2combo.Column(1, Me.c8combo.ListIndex)
ccombo(9) = Me.c2combo.Column(1, Me.c9combo.ListIndex)
ccombo(10) = Me.c2combo.Column(1, Me.c10combo.ListIndex)
ccombo(11) = Me.c2combo.Column(1, Me.c11combo.ListIndex)
ccombo(1) = Get_PostCode_Score(Replace(Me.TextBox1, " ", ""))

Total = 0
For i = 2 To 11
    Total = Total + ccombo(i)
Next
Me.total_score = Total
DoCmd.Close acForm, "calcsinprog", acSaveNo
End Sub

Any help at all will be greatly appreciated

All replies (10)

Friday, February 3, 2006 6:22 PM ✅Answered | 2 votes

If you really want to pause...

Sytem.Threading.Thread.Sleep(1000)

will pause for 1000 ms

or

My.Application.Doevents

Doevents will process all requests in the windows messages queue.   

 

 

 

Personally the doevents is the one I would choose as intentionally putting delays in there is rather a subjective decision - 1000 ms might work fine on the machine you using but on a different machine running different configuration it may not be enough.   Processing all requests before continuing is probably what is required here.

 


Friday, February 3, 2006 4:32 PM

This may seem intuitive, but, put a DoEvents after your OpenForm, before your calculations start.

 

Tim


Friday, February 3, 2006 6:30 PM

 

Spotty,

In my experience, sleep doesn't allow the paint routines to actually refresh screens.

DoEvents relinquishes execution to other threads that need to run.

The other day I had a client with exactly this problem. I didn't even have my glasses, and couldn't see the code. I asked some questions, he was doing calculations with a database exactly like this and his screen weren't updating.

"Do you have any nested for loops", I asked. If so, put a doevents on the outside of the for loops, so you won't excute it too often.

He did, and his application came right to life.

 


Tuesday, April 7, 2009 5:41 PM

The solution is simple. Create a new sub called "wait" (or watever you like). This will cause the pc to wait about 5 sec on a pentium 4 PC. Please do not use 288s, 286s, 386s, 486s or pentium mmx. Those dinausaures are outadated.

Sub Wait()
    For waiting_availability = 5000 To 1 Step -1
        DoEvents
        ' insert text box here if you want to display the countdown from 5000 to 1
    Next
End Sub

Then call the sub wait from Calc_Click as follow:

Private Sub Calc_Click()
Dim ccombo(11) As Integer
Dim Total As Integer
DoCmd.OpenForm "calcsinprog"

Call Wait

ccombo(2) = Me.c2combo.Column(1, Me.c2combo.ListIndex)
ccombo(3) = Me.c2combo.Column(1, Me.c3combo.ListIndex)
ccombo(4) = Me.c2combo.Column(1, Me.c4combo.ListIndex)
ccombo(5) = Me.c2combo.Column(1, Me.c5combo.ListIndex)
ccombo(6) = Me.c2combo.Column(1, Me.c6combo.ListIndex)
ccombo(7) = Me.c2combo.Column(1, Me.c7combo.ListIndex)
ccombo(8) = Me.c2combo.Column(1, Me.c8combo.ListIndex)
ccombo(9) = Me.c2combo.Column(1, Me.c9combo.ListIndex)
ccombo(10) = Me.c2combo.Column(1, Me.c10combo.ListIndex)
ccombo(11) = Me.c2combo.Column(1, Me.c11combo.ListIndex)
ccombo(1) = Get_PostCode_Score(Replace(Me.TextBox1, " ", ""))

Total = 0
For i = 2 To 11
    Total = Total + ccombo(i)
Next
Me.total_score = Total
DoCmd.Close acForm, "calcsinprog", acSaveNo
End Sub

Sub Wait()
    For waiting_availability = 5000 To 1 Step -1
        DoEvents
        ' insert text box here if you want to display the countdown from 5000 to 1
    Next
End Sub

Then just keep on calling "wait" where ever else you need a pause. That's as good as it gets with pause in VB. Hope this help.


Tuesday, April 7, 2009 8:24 PM

@Pegasuseg519 - Great suggestion! How about incorporating spotty7889409's idea, and give your sub a parameter...like counts to wait? That way, you can adjust the delay in other code, so that the delay is neither too long, nor too short. In fact, if you tied it into the .NET equivalent of GetTickCount, you could set it in a system independent manner. GetTickCount uses (rough) milliseconds.


Tuesday, April 7, 2009 9:13 PM

Fine, if you're trying to make a space heater out of your computer.  Use a timer so you're in a SystemIdleState while waiting.


Saturday, October 15, 2016 10:50 PM

I have found an error in your code!

System.Threading.Thread.Sleep(1000)

You forgot the 2nd S in System!


Saturday, October 15, 2016 11:02 PM | 2 votes

I have found an error in your code!

System.Threading.Thread.Sleep(1000)

You forgot the 2nd S in System!

You found an error in code in a 7 yoa thread. That's nice

La vida loca


Sunday, July 2, 2017 8:03 AM

A little typo in there...

It would be:

System.Threading.Thread.Sleep(1000)


Saturday, April 21, 2018 9:34 AM

Most Be : 

System.Threading.Thread.Sleep(1000)