View Full Version : Function called after, not before loop


supercharge
01-05-2006, 01:28 PM
Hi all,

Does anyone have any idea why a function does not get called before a loop such as a for loop eventhough the Call is placed ahead of the loop?

Example: Function ShowWait is placed before the For loop but when you run the example (attached), the calculation takes about 1 second but during that time, the status box's backcolor and text should be changed according to the function below. Right now, it happens only after the completion of the loop.


Private Sub cmdCal_Click()
UpdateConfirm = MsgBox("Are you sure you want to do this? Click Yes to confirm!", vbYesNo)
If UpdateConfirm = vbYes Then
'ShowWait Function not being called 'till after the for loop????????
ShowWait
''''''''
For w = 1 To 1000 Step 0.025
A = 1000 / w
txtResult.Value = A
Next w
Else
Exit Sub
End If
WorkDoneAlert = MsgBox("Operation Completed!")
HideWait
End Sub

Function ShowWait()
txtStatus.BackColor = RGB(255, 0, 255)
txtStatus.Value = "Updating..."
End Function

Function HideWait()
txtStatus.BackColor = RGB(0, 128, 64)
End Function

Thanks all in advance.

Sergeant
01-05-2006, 01:42 PM
Private Sub cmdCal_Click()
UpdateConfirm = MsgBox("Are you sure you want to do this? Click Yes to confirm!", vbYesNo)
If UpdateConfirm = vbYes Then
'ShowWait Function not being called 'till after the for loop????????
ShowWait
''''''''
DoEvents <<< Add This, and see what happens.
For w = 1 To 1000 Step 0.025
A = 1000 / w
txtResult.Value = A
Next w
Else
Exit Sub
End If
WorkDoneAlert = MsgBox("Operation Completed!")
HideWait
End Sub

Function ShowWait()
txtStatus.BackColor = RGB(255, 0, 255)
txtStatus.Value = "Updating..."
End Function

Function HideWait()
txtStatus.BackColor = RGB(0, 128, 64)
End Function

supercharge
01-06-2006, 05:53 AM
Adding that works out great. Thank you.

I'd still like to know why it isn't called before the looop though. I understand that adding DoEvents really forces it to do so but logically, the function should be called first then the loop. I also do not think that using DoEvents is the perfect solution but it does the job.

Thank you again.