Hey all,

I have a problem with this particular piece of coding I have for my payroll system in Visual Basic 6. I am basically trying to calculate the wages of employee's by entering in the number of hours they have worked. The criteria of how much pay they receive works like this:

If they work the standard 40 hours or less, they get paid number of hours x $5.5
e.g. 40 hours x $5.50 = $220

If they work between 41-50 hours (which is considered as overtime), they get 40 hours of pay PLUS an extra $1.50 per hour
e.g. 45 hours (40 hours x $5.50) + (5 additional hours x $5.50 x $1.50) = $261.25

If they work between 51-60 hours (again considered as overtime), they get an extra $2.50 an hour, while adding the other $1.50 per hour they have worked for the other overtime hours
e.g. 52 hours (40 x $5.50) + (10 additional hours x $5.50 x $1.50) + (2 additional hours x $5.50 x $2.50) = $330

I have written this code up to calculate this:

Private Sub cmdCalculate_Click()
    Hours = txthoursworked
    
    If Hours > 0 <= 40 Then
    lblgrosspay = (Hours * 5.5)
    ElseIf Hours > 41 <= 50 Then
    lblgrosspay = (Hours * 5.5) * 1.5
    ElseIf Hours > 51 <= 60 Then
    lblgrosspay = (Hours * 5.5) * 2.5
    End If

But this code is not working properly.

If I enter anything between 0 to 40, the code works properly (e.g. 40 hours -> $220). But if I enter anything more, such as 47 hours, it does not calculate the additional overtime pay that should be added onto it (e.g. 47 hours should be $277.75) but it keeps coming up as $258.50 (not multiplying the extra 1.5).

I think the code I have written quite wrong, but I do not understand how I can fix it up. I think an extra overtime box may be required, but I am not sure how I can do that either. Any help would be appreciated, thanks.

Recommended Answers

All 10 Replies

>If they work between 41-50 hours (which is considered as overtime), they get 40 hours of pay PLUS an extra $1.50 per hour
e.g. 45 hours (40 hours x $5.50) + (5 additional hours x $5.50 x $1.50) = $261.25
OverHours = (Hours - 40)
Hours = 40
GrossPay = (Hours * 5.5 ) + (OverHours * 5.5 * 1.5)

and

WellOverHours = (Hours - 50)
OverHours = (Hours - (40 + WellOverHours))
Hours = 40
GrossPay = (Hours * 5.5 ) + (OverHours * 5.5 * 1.5) + (WellOverHours * 5.5 * 2.5)

Good Luck

Thanks for that vb5prgrmr, but I don't think I have entered it properly. Can you help me implement the coding within what I have got? I tried entering it with some of the coding I have but now the original and the overtime hours do not calculate properly.

The answer is there already... I don't know what else I can do except do your work for you!

Ah OK I understand what you meant now, however there is a problem when I enter the code. It works fine for calculating the normal pay and the first overtime pay, but when I change the GrossPay to lblgrosspay (which is where it will be displayed), anything I enter in the hours worked box will have the end result of "248" gross pay. How can I fix this? Thanks.

Without seeing your code... It is kind of hard to tell where you went wrong or where you missed something...

Good Luck

This is what I have:

Private Sub txtnumberofhours_Change()
    Hours = txtnumberofhours
    
    OverHours = (Hours - 40)
Hours = 40
lblendgross = (Hours * 5.5) + (OverHours * 5.5 * 1.5)

WellOverHours = (Hours - 50)
OverHours = (Hours - (40 + WellOverHours))
Hours = 40
lblendgross = (Hours * 5.5) + (OverHours * 5.5 * 1.5) + (WellOverHours * 5.5 * 2.5)

I am trying to get the end result in the lblendgross label. When I enter this, the end result is always 248, no matter if I enter 1 or 60. However, if I enter this:

Private Sub txtnumberofhours_Change()
    Hours = txtnumberofhours
    
    OverHours = (Hours - 40)
Hours = 40
lblendgross = (Hours * 5.5) + (OverHours * 5.5 * 1.5)

WellOverHours = (Hours - 50)
OverHours = (Hours - (40 + WellOverHours))
Hours = 40
GrossPay = (Hours * 5.5) + (OverHours * 5.5 * 1.5) + (WellOverHours * 5.5 * 2.5)

The normal 40 hours of pay and the overtime upto 50 hours works properly, which is because I am not using the formula to calculate it in the end gross label. How can I fix this? Thanks

Where is your if hours <= 40 then, elseif hours > 40 and hours < 50 checks?

I had deleted it, but if I insert it would look like this:

Private Sub txtnumberofhours_Change()    
    Hours = txtnumberofhours
    
    If Hours > 0 <= 40 Then
    lblendgross = (Hours * 5.5)
    ElseIf Hours > 41 <= 50 Then
    lblendgross = (Hours * 5.5) * 1.5
    ElseIf Hours > 51 <= 60 Then
    lblgendgross = (Hours * 5.5) * 2.5
    End If
    
    OverHours = (Hours - 40)
Hours = 40
lblendgross = (Hours * 5.5) + (OverHours * 5.5 * 1.5)

WellOverHours = (Hours - 50)
OverHours = (Hours - (40 + WellOverHours))
Hours = 40
lblendgross = (Hours * 5.5) + (OverHours * 5.5 * 1.5) + (WellOverHours * 5.5 * 2.5)
End Sub

But it still does not work.

Okay, you have to put the forumulas I showed you, in the right places within that if statement...

Also Lines 6 and 8. I want you to look closely at and look at the logic of what you have going on with those if checks. Yes, I am hinting that something is wrong with your logic...

also Line2 should be Hours = Int(txtnumberofhours.text)

You need to slow down and think of the logic going on here because you have had all the information for a couple of days now and it just seems like you are wanting someone to do your work for you. You have the pieces, now just assemble them in the correct order, and you will have your solution.

Good Luck

Ahh thank you, I managed to find out where I was going wrong. Thanks for your help again :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.