PDA

View Full Version : More help with visual basic


George Hallam
10-23-2008, 10:05 AM
Ok the question i have is

The shipshape packing company wants a program for their orders department to calculate and display the price of an order. The order clerks enter the number of units ordered, weather a customer is a wholesaler or a retailer, weather or not the customer is a special customer. Use a variety of controls for this data entry. The price the customer pays per unit depends on these three things the prices are

for wholesaler
1-5 units = $50
6-10 units = $45
11-20 units = $40
21-50 units = $35
over 50 units = $30

for retailers
1-3 units = $60
4-8 units = $55
9-15 units = $50
16-40 units = $45
over 40 units = $40

special customers get a discount of 10%


now i have a problem here is my code so far and the design

Option Explicit
Dim number_of_units As Integer
Dim whole_sale_price As Single
Dim retail_price As Single
Dim total As Single
Dim specail As Single


Private Sub cmdcalc_Click()
number_of_units = txtunits.Text
If number_of_units <= 5 Then
If optwhole.Value = True Then
whole_sale_price = 50
txttotal.Text = number_of_units * whole_sale_price
Else
If number_of_units <= 3 Then
If optretail.Value = True Then
retail_price = 60
txttotal.Text = number_of_units * retail_price
End If
End If
End If
Else
If number_of_units <= 10 Then
If optwhole.Value = True Then
whole_sale_price = 45
txttotal.Text = number_of_units * whole_sale_price
Else
If number_of_units <= 8 Then
If optretail.Value = True Then
retail_price = 55
txttotal.Text = number_of_units * retail_price
End If
End If
End If
Else
If number_of_units <= 20 Then
If optwhole.Value = True Then
whole_sale_price = 40
txttotal.Text = number_of_units * whole_sale_price
Else
If number_of_units <= 15 Then
If optretail.Value = True Then
retail_price = 50
txttotal.Text = number_of_units * retail_price
End If
End If
End If
Else
If number_of_units <= 50 Then
If optwhole.Value = True Then
whole_sale_price = 35
txttotal.Text = number_of_units * whole_sale_price
Else
If number_of_units <= 40 Then
If optretail.Value = True Then
retail_price = 45
txttotal.Text = number_of_units * retail_price
End If
End If
End If
If number_of_units > 50 Then
If optwhole.Value = True Then
whole_sale_price = 30
txttotal.Text = number_of_units * whole_sale_price
Else
If number_of_units > 40 Then
If optretail.Value = True Then
retail_price = 40
txttotal.Text = number_of_units * retail_price
End If
End If
End If
End If
End If
End If
End If
End If
End Sub



http://i289.photobucket.com/albums/ll221/jiggyghallam/Customer.jpg

The problem i have is that certain values just don't work, ie

52 and pressing calculate just doesn't bring up anything (doesn't work)
4 works for wholesaler but not for retailer
3 works with both
21 works with both
18 works with wholesaler but not for retailer

what is the problem? how can i fix it? how can i make it so it doesnt have so many if statements?

any more questions please ask ;)

im using VB 6

im so confused i know my code is messed up there are to many ifs and i cant simplify it any help would be greatly appreciated :D :rolleyes:

knock knock Paul Komski ;)

~George :D

classicsoftware
10-23-2008, 01:55 PM
Do you have to do this in VB?

George Hallam
10-23-2008, 01:56 PM
sadly yes :(

Paul Komski
10-24-2008, 06:16 AM
I would first use just use one if/else/end if to separate wholesalers from retailers.

Within each block (wholesaler else retailer) use Select Case statements for the various unit quantities.

Then have an if/end if to apply the discount for special customers.

And finally an if/end if to calculate the total or better still use a separtate function to do the calculation.


Always try to avoid nesting if statements. Better to separate them and recalculate the variable and learn how to create your own customised functions. Simplify complicated functions to call other simple functions.

You could try:

Option Explicit

Public Function TotalCost(Wholesaler As Boolean, Units As Long, SpecialClient As Boolean) As Single

If Wholesaler Then

Select Case Units
Case Is < 6
TotalCost = 50 * Units
Case 6 To 10
TotalCost = 45 * Units
Case 11 To 20
TotalCost = 40 * Units
Case 21 To 50
TotalCost = 35 * Units
Case Else
TotalCost = 30 * Units
End Select

Else

Select Case Units
Case Is < 4
TotalCost = 60 * Units
Case 4 To 8
TotalCost = 55 * Units
Case 9 To 15
TotalCost = 50 * Units
Case 16 To 40
TotalCost = 45 * Units
Case Else
TotalCost = 40 * Units
End Select


End If

If SpecialClient Then
TotalCost = TotalCost * 0.9
End If

End Function



Private Sub cmdcalc_Click()

txttotal.Text = TotalCost(optwhole.Value, txtunits.Text, specialcustomer.Value)

End Sub

I had to guess the specialcustomer name so edit appropriately.

George Hallam
10-24-2008, 09:20 AM
woah :)

thanks you sooo much again Paul Komski no doubt i will be back :p

~George :D