Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DoEvents command 2

Status
Not open for further replies.

afryer

Programmer
Mar 9, 2004
207
GB
Hi All,

I have an application which runs in a loop, processing data from a database and updating a progress bar. I have used to DoEvents command to allow a cancel button to pressed to cancel the operation if it is taking too long.

It the btn_cancel even I ask the user if they wish to cancel and if they select yes then I wish to unload the current form using Unload Me. What I am finding though is the code still returns to the loop and continues trying to read from the (now closed) database. Is there anyway I can prevent this happening?

Thanks

Andrew
 
You will need to set some sort of cancel boolean flag on your (or globablly) that the loop will need to check to see if it needs to cancel. When the button is pressed you set the canel flag to true. In your loop if the cancel flag is true then do an 'Exit Do/For' and then continue your ending process from there.
 
try it this way


While Not .EOF
ProgressBar.Max = .RecordCount
ProgressBar.Value = .AbsolutePosition
frmPrintQRPage2.StatusBar1.SimpleText = "Printing QR, Pls wait... | " & .AbsolutePosition & " of " & .RecordCount
DoEvents
frmPrintQRPage2.fPrintQR !fld_c_ContainerNo, !fld_c_partNo, !fld_c_ItemCode, !fld_c_MakerCode, !fld_c_InvoiceNo, _
!fld_c_ArrivalDate, !fld_c_OrderNo, !fld_c_PalletNo, !fld_c_BinLocation, !fld_i_CartonOf, !fld_i_TotalCarton, _
!fld_i_PageOf, !fld_i_TotalPage, !fld_i_PackQty, !fld_c_Series
.MoveNext
Sleep g_Session.PrintDelay
If mCancelPrinting Then
If fMsgBox("Cancel Printing?", vbExclamation + vbYesNo, "Print QR") = vbYes Then
frmPrintQRPage2.cmdPrint.Enabled = True
frmPrintQRPage2.cmdPrintIndividual.Enabled = True
frmPrintQRPage2.cmdCancel(0).Enabled = True
frmPrintQRPage2.pSetContainerPrinted
Me.pictCompleted.Visible = frmPrintQR.mRS_Containers!fld_c_PrintedDesc = "Complete"
mCancelPrinting = False
Exit Sub
End If
End If
mCancelPrinting = False
Wend
 
sory 4 my preious post...

dim gcancel as boolean


Private Sub cmdCancel_Click()
gcancel = true
End Sub


private sub pProcess
gCancel = false
while not gCancel
' processsssssss
wend
if gCancel then msgbox "PROCESS CANCELLED!"
end sub


WALA


:)

 
Er....your code doesn't quite work, rm, if you're running through a recordset. Also, there are a couple of minor inefficiencies. This is a little cleanup, more closely following bj's recommendations:
Code:
dim gcancel as boolean

Private Sub cmdCancel_Click()
   gcancel = true
End Sub

private sub pProcess
with myRs
   do until .eof
      DoEvents 'will set gCancel if Cancel button has been pressed
      if gCancel then
         gCancel = False 
         Exit Sub 'or exit do as bj suggests, depends on context
      End If
      'processsssssss
   Loop
end with
end sub

As for your code, rm:
1. If you loop around the gCancel, you'll never leave the loop if you don't press cancel. Assuming that the loop iterates through a recordset, you'll have an eof error when you get done.
2. No sense setting the gCancel to false if it already is false, so better to set it as a consequence of its having been set to true.
3. No need to reevaluate gCancel outside of the loop.

HTH

Bob

p. s. Does "WALA" mean "voila"? I've noticed it in a few of your posts. :)
 
An optimization might be to use a counter variable and increment it every iteration of the loop. Once the counter reaches an appropriate value (10, 100, 1000, it depends) you can reset the counter to zero and call DoEvents.

Adjusting this trigger point allows you to keep your application responsive to it's "cancel" button yet won't slow the program so much you've given the user something else to complain about. You may need to tweak the trigger point (frequency) limit value based on how long it takes you to process each item of the array/collection/recordset you are looping over.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top