Function called after, not before loop (1 Viewer)

supercharge

Registered User.
Local time
Today, 14:13
Joined
Jun 10, 2005
Messages
215
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.

Code:
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.
 

Attachments

  • Trial.zip
    14.2 KB · Views: 260

Sergeant

Someone's gotta do it
Local time
Today, 17:13
Joined
Jan 4, 2003
Messages
638
Code:
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 [COLOR="Red"]<<< Add This, and see what happens.[/COLOR]
        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

Registered User.
Local time
Today, 14:13
Joined
Jun 10, 2005
Messages
215
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.
 

Users who are viewing this thread

Top Bottom