From: asrul on
Hi All,
I run a repeated code (For Each..Next statement) in my form
and calling a module.
But it seems my module runs slower than the code in
my form,how do I can add a wait time in my form code
so that it can waits untill the module completely done to move
to the next task.

Regards,


From: Paul Shapiro on
I use the Windows API to sleep, with a routine that "wakes" periodically to
run DoEvents and let the Access UI update. I use 100 milliseconds as the
time to sleep between calls to DoEvents, but you can change that in the code
below if you prefer.

'Windows API declaration
Private Declare Sub pjs_apiSleep Lib "kernel32" Alias "Sleep" (ByVal
dwMillisec As Long)

'VBA routine to pause execution without burning cpu cycles
Sub pjsWait(lngMilliSecs As Long) 'Pauses execution for the specified
number of milliseconds
Dim lngSleepCycle As Long, lngSleepTotal As Long, lngSleepLeft As Long
Const clngMaxSleep As Long = 100 'Maximum millisecs without waking up
to DoEvents

lngSleepTotal = 0
Do While lngSleepTotal < lngMilliSecs
lngSleepLeft = lngMilliSecs - lngSleepTotal
If lngSleepLeft > clngMaxSleep Then
lngSleepCycle = clngMaxSleep
Else
lngSleepCycle = lngSleepLeft
End If

pjs_apiSleep dwMillisec:=lngSleepCycle
lngSleepTotal = lngSleepTotal + lngSleepCycle
DoEvents
Loop

Exit Sub
End Sub

"asrul" <asrul(a)invalid.com> wrote in message
news:e$Hc8brLKHA.5948(a)TK2MSFTNGP05.phx.gbl...
> Hi All,
> I run a repeated code (For Each..Next statement) in my form
> and calling a module.
> But it seems my module runs slower than the code in
> my form,how do I can add a wait time in my form code
> so that it can waits untill the module completely done to move
> to the next task.

From: Mike Painter on
asrul wrote:
> Hi All,
> I run a repeated code (For Each..Next statement) in my form
> and calling a module.
> But it seems my module runs slower than the code in
> my form,how do I can add a wait time in my form code
> so that it can waits untill the module completely done to move
> to the next task.
>
> Regards,

DoEvents is what you want.
It is not a timer, it just stops things until all the events prior are
finished.

It solves the occasional problem with event driven programs.


From: Banana on
I'm not sure if DoEvents is a good idea.

DoEvents' purpose is to relinquish control to the system to allow other
tasks in queue to processing. However when used as a ad hoc 'wait', it
actually can make things even more slower because it will be checking
and thus bothering the system when it tries to do something.

Four options comes to mind:

1) Use DoCmd.Hourglass to change the pointer to hourglass. You may
already have done that, but I included this for completeness.

2) Turn off Application.Echo which will suspend the screen drawing. The
input also will not be accepted during that, which is useful for a brief
period, but if it's turned off for too long, it may lead users to think
it's frozen and give the application the good ol' three fingered salute.
But if used in conjunction with #3 or maybe just StatusBarText so the
user know it's doing some background processing, it's good way to speed
up the processing.

3) Use a modal & popup dialog form to communicate to the user that a
task is processing. The input will also not be accepted and this
provides more feedback to the users.

4) Use a API call. There is a API for "Sleep", which I'm sure if you
google for "Sleep API Visual Basic", you can find the declaration and
use it to wait for X milliseconds.

That said, it should be unnecessary to need to sleep or wait with
properly written code, even for tasks that may be executed
asynchronously. For that reason, I usually would take a step back and
re-think about how it can be re-written in a way that it's either
handled already or is nonissue.

HTH.

Mike Painter wrote:
> asrul wrote:
>> Hi All,
>> I run a repeated code (For Each..Next statement) in my form
>> and calling a module.
>> But it seems my module runs slower than the code in
>> my form,how do I can add a wait time in my form code
>> so that it can waits untill the module completely done to move
>> to the next task.
>>
>> Regards,
>
> DoEvents is what you want.
> It is not a timer, it just stops things until all the events prior are
> finished.
>
> It solves the occasional problem with event driven programs.
>
>
From: asrul on
Dear Paul,
Thank you for assisting,but I'm still not sure how to do that
I'm still noobeso will take a few times,to absorb your
explanation.and I'm sorry for keep asking.

Can you say where I have to put my code?.
Currently,variable item of my For each..next statement,
came from the selected items in my multi select list box.

And what I want to achieve is.
Select the item in the multiselect listbox, then run an on the fly
make a table query,then print the report to PDF with lebans code
and continue with the next item in the multi select listbox.

The code works fine for 1 selected item,but for multi select it can
print 1 the last selected item only,although I can see access output
the snapshot file for several times,the lebans code just output the
last report only.



"Paul Shapiro" <paul(a)hideme.broadwayData.com> wrote in message
news:ePtonOvLKHA.3276(a)TK2MSFTNGP06.phx.gbl...
> I use the Windows API to sleep, with a routine that "wakes" periodically
to
> run DoEvents and let the Access UI update. I use 100 milliseconds as the
> time to sleep between calls to DoEvents, but you can change that in the
code
> below if you prefer.
>
> 'Windows API declaration
> Private Declare Sub pjs_apiSleep Lib "kernel32" Alias "Sleep" (ByVal
> dwMillisec As Long)
>
> 'VBA routine to pause execution without burning cpu cycles
> Sub pjsWait(lngMilliSecs As Long) 'Pauses execution for the specified
> number of milliseconds
> Dim lngSleepCycle As Long, lngSleepTotal As Long, lngSleepLeft As Long
> Const clngMaxSleep As Long = 100 'Maximum millisecs without waking
up
> to DoEvents
>
> lngSleepTotal = 0
> Do While lngSleepTotal < lngMilliSecs
> lngSleepLeft = lngMilliSecs - lngSleepTotal
> If lngSleepLeft > clngMaxSleep Then
> lngSleepCycle = clngMaxSleep
> Else
> lngSleepCycle = lngSleepLeft
> End If
>
> pjs_apiSleep dwMillisec:=lngSleepCycle
> lngSleepTotal = lngSleepTotal + lngSleepCycle
> DoEvents
> Loop
>
> Exit Sub
> End Sub
>
> "asrul" <asrul(a)invalid.com> wrote in message
> news:e$Hc8brLKHA.5948(a)TK2MSFTNGP05.phx.gbl...
> > Hi All,
> > I run a repeated code (For Each..Next statement) in my form
> > and calling a module.
> > But it seems my module runs slower than the code in
> > my form,how do I can add a wait time in my form code
> > so that it can waits untill the module completely done to move
> > to the next task.
>