VB6 Help

Gautama2

Golden Member
Jun 13, 2006
1,461
0
0
Dim intNumOfEmployees As Integer
Dim intAvg As Integer
Dim intTotalPay As Integer
Dim intNumOfPieces As Integer
Dim intTotalPieces As Integer

Private Sub mnuFileCalc_Click()
intNumOfPieces = Val(txtPieces)
lblPay = FormatCurrency(Pay(intNumOfPieces))
intNumOfEmployees = intNumOfEmployees + 1
intTotalPay = val(lblPay) + intTotalPay
intAvg = intNumOfEmployees / intTotalPay
intTotalPieces = intNumOfPieces + intTotalPieces
End Sub

Private Function Pay(intNumOfPiecesPASSED As Integer) As Currency
If intNumOfPiecesPASSED >= 1 And intNumOfPiecesPASSED <= 199 Then
Pay = intNumOfPiecesPASSED * 0.5
End If

If intNumOfPiecesPASSED >= 200 And intNumOfPiecesPASSED <= 399 Then
Pay = intNumOfPiecesPASSED * 0.55
End If

If intNumOfPiecesPASSED >= 400 And intNumOfPiecesPASSED <= 599 Then
Pay = intNumOfPiecesPASSED * 0.6
End If

If intNumOfPiecesPASSED >= 600 Then
Pay = intNumOfPiecesPASSED * 0.65
End If
End Function

intAvg = intNumOfEmployees / intTotalPay
is giving me division by zero error, what am I missing?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
There's no way for us to know why intTotalPay has a value of 0. As the others suggested, you'll have to step through your logic and figure it out. Just glancing at it I wondered what value Pay() would return if intNumOfPiecesPassed were <= 0. Also, what is the effect of val(FormatCurrency(Pay(intNumOfPieces))) (which is what you're effectively doing)?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
If the number of pieces is zero, you would be causing a divide by zero error.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
lblPay = FormatCurrency(Pay(intNumOfPieces))

Based on Hungarian notation, you're setting a label object to a string object.

Therefore,

intTotalPay = val(lblPay) + intTotalPay

Is going to have the same issues. lblPay is most likely a label object where you'd want lblPay.Text (or is it Caption? I can never remember). Passing in lblPay will not result in an error, because the string representation of an object is the object's derivation (or something like that) so it's probably doing something along the line of val("System.Windows.Form.Label") which has no numbers in it. Also, val is not going to work out of the box with decimals as if you do val("10.50") you will get 1050 back. You can multiply by .01 and get the number you're really looking for.