Results 1 to 11 of 11

Thread: Calculations [VB6] [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Resolved Calculations [VB6] [RESOLVED]

    I am writing a program for a friend - here are the details for how the pricing works - how do i do this in VB as i am not very good with calculations etc in vb

    Minimum Charge or $5, which covers 10 kgs for 5 kms
    Each kg over 10 kgs incurs a charge of $0.50
    Distances over 5kms incur an additional charge of $1.00 per km up to 10 kms
    For deliveries over 10 kms, the extra distance should be rounded up to the nearest 5 kms and charged at $2.00 for each extra 5 km block – e.g. if the total distance is 23.75 kms, the extra charge is $5.00 for kms 5 to 10, plus $2.00 x 3(13.75/5) = $11.00

    IntWeight = package weight
    IntDistance = distance

    Thanks
    Last edited by nzguy2004; Jun 22nd, 2005 at 10:42 PM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculations [VB6]

    I think this is what you want. Let me know if it's not right.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim Weight As Integer, Distance As Integer, Charge As Currency
    3.     Dim AddedUnits As Integer
    4.     Weight = Val(Text1.Text)
    5.     Distance = Val(Text2.Text)
    6.    
    7.     Select Case Weight
    8.       Case 0 To 10
    9.         Charge = 5
    10.       Case Else
    11.         Charge = 5 + (0.5 * (Weight - 10))
    12.     End Select
    13.  
    14.  
    15.     Select Case Distance
    16.     Case Is < 5.001
    17.     ' No Extra Charge
    18.     Case 5.001 To 10
    19.       Charge = Charge + Distance - 5 * 1
    20.     Case Is > 10
    21.     ' round to nearest 5
    22.       If Distance / 5 = Int(Distance / 5) Then
    23.         AddedUnits = (Distance - 10) / 5
    24.       Else
    25.         AddedUnits = Distance - 10 ' 23.75 - 10 = 13.75
    26.         AddedUnits = (AddedUnits + 5 - (AddedUnits Mod 5)) / 5
    27.         '   13.75 + 5 <18.75> - 13.75 Mod 5 <3.75> = 15 / 5 = 3
    28.       End If
    29.       Charge = Charge + (AddedUnits * 2)
    30.     End Select
    31.  
    32.     Text3.Text = Format(Charge, "Currency")
    33. End Sub
    34.  
    35. Private Sub Form_Load()
    36.     Text1.Text = "" ' Weight
    37.     Text2.Text = "" ' Distance
    38.     Text3.Text = "" ' Cost
    39. End Sub
    Last edited by dglienna; Jun 21st, 2005 at 12:22 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Calculations [VB6]

    How would i modify it so that the output was to lblWeight, Distance, TotalCost
    and it got its data from intDistance and intWeight?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculations [VB6]

    Something like this


    VB Code:
    1. Weight = intWeight
    2.     Distance = intDistance
    3. ' Do Calcs
    4. '
    5.      lblWeight = Weight
    6.      lblTotalCost = Format(Charge, "Currency")    
    7.      lblDistance = Distance

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Calculations [VB6]

    if intweight and intdistance are interger, they will always round down, where you really want to always round up, if it is 10.2 it is over 10 so sould be 11

    VB Code:
    1. Dim intweight As Single, intdistance As Single rwt as sin
    2. Dim rwt As Single, rdist As Single, wcharge As Single, dcharge As Single, charge As Single
    3. charge = 5
    4. intdistance = 23.75
    5. rdist = intdistance \ 1
    6. 'Round UP
    7. If rdist < intdistance Then rdist = rdist + 1
    8. intweight = 10.5
    9. rwt = intweight \ 1
    10. If rwt < intweight Then rwt = rwt + 1
    11.  
    12. Select Case rwt
    13.         Case Is <= 10: wcharge = 0
    14.         Case Is > 10: wcharge = 0.5 * (rwt - 10)
    15. End Select
    16. Select Case rdist
    17.         Case Is <= 5: dcharge = 0
    18.         Case 5 To 10: dcharge = 1 * (rdist - 5)
    19.         Case Is > 10: dcharge = 5 + (rdist - 10) \ 5 * 2
    20. End Select
    21.  
    22. charge = charge + wcharge + dcharge
    23. MsgBox "Weight = " & intweight & ": Distance = " & intdistance & ": Total charge = " & charge

    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Calculations [VB6]

    Thankyou to both of you. I will try both when i get a chance and let you know

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Calculations [VB6]

    Here is an example which neither code worked for, or are my calculations incorrect?

    Weight:33 Distance:23
    Excess Weight Cost:11.50
    18kms (5 to 10 kms @ 1.00/km) 5.00
    13 kms = 6.00
    Total:27.50

    Program: 22.50

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Calculations [VB6]

    ok fixed that, try some more to make sure no other bugs

    VB Code:
    1. Dim intweight As Single, intdistance As Single
    2. Dim rwt As Single, rdist As Single, wcharge As Single, dcharge As Single, charge As Single
    3. charge = 5
    4. intdistance = 23
    5. rdist = intdistance \ 1
    6. 'Round UP
    7. If rdist < intdistance Then rdist = rdist + 1
    8. intweight = 33
    9. rwt = intweight \ 1
    10. If rwt < intweight Then rwt = rwt + 1
    11.  
    12. Select Case rwt
    13.         Case Is <= 10: wcharge = 0
    14.         Case Is > 10: wcharge = 0.5 * (rwt - 10)
    15. End Select
    16. Select Case rdist
    17.         Case Is <= 5: dcharge = 0
    18.         Case 5 To 10: dcharge = 1 * (rdist - 5)
    19.         Case Is > 10
    20.               ed = (rdist - 10) \ 5
    21.               If ed * 5 < rdist Then ed = ed + 1
    22.               dcharge = 5 + ed * 2
    23.              
    24. End Select
    25.  
    26. charge = charge + wcharge + dcharge
    27. MsgBox "Weight = " & intweight & ": Distance = " & intdistance & ": Total charge = " & charge

    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Calculations [VB6]

    Perfect Thankyou very much

    I think that lblTotalCost = Format(charge, "Currency")
    Will add the decimal places etc?

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculations [VB6]

    Yes, to your localization settings on the computer.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    13

    Re: Calculations [VB6]

    Thanks to westconn1 and dglienna

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width