By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Creating a Shipping Charge Calculator

P: 26
Hello everybody. I am a complete noob in writing VB code. I don't have a problem drawing a form, but when it comes to code. I am lost.
I have a user interface design class and we have a small project due. I was wondering if anyone could give me some mega help.
The project we have to complete is the following:

The Fast Freight Shipping Company charges the rates listed in the following table:

Weight of the Package (in kilograms) / Shipping rate per Mile
2kg or less / $0.01
over 2kg, but not more than 6kg /$0.015
over 6kg, but not more than 10kg /$0.02
over 10kg, but not more than 20kg / $0.025

Create an application that allows the user to enter the weight of the package and the distance it is to be shipped, and then displays the charges.

Input validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more thann 20 kg(this is the maximum weight the company will ship) Do not accept distances of less than 10 miles or more than 3000 miles. These are the company's minimum and maximum shipping distances. Use exception handling to check for non-numeric data.



I'm knd of asking for someone to pretty much write the code I would need to make this function properly. I will be trying to mess around with it while I wait for any response. Thanks very much in advancne
Mar 27 '07 #1
Share this Question
Share on Google+
19 Replies


Dököll
Expert 100+
P: 2,364
Ahhh, the good old days :-) Hello rents!

I have had similar projects in the past..I am sure likewise for members here. Would you be able to post what you have thus far so one can take a look?

It'd be helpful to you and to this forum if in need of a quick response...

When is this due?

Dököll
Mar 28 '07 #2

P: 26
Dokoll,

Thanks for the response. So far, I have only created the form itself. With buttons and labels and what not. The only code I have entered is for the exit button to work :)

I'm not exactly sure what you like to me show on here, so I just copied and pasted the part where the code is placed.

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Exit Calculator

Me.Close()

End Sub

Private Sub txtWeight_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWeight.TextChanged

End Sub

Private Sub txtDistance_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDistance.TextChanged

End Sub

Private Sub lblCost_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCost.TextChanged

End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

End Sub
End Class

i guess this would give you an idea as to what I have on the form. I feel a little silly not knowing even where to begin to write code. I mean I know where it goes, but just confused as to what to type in first.

If you need more info from me, please let me know. This project is due on Saturday, but I'd like to have it finished by at least Friday.
Thanks
Mar 28 '07 #3

Dököll
Expert 100+
P: 2,364
OK, Rents...Thanks for your prompt response. I have a simple code you can add to the mix. Let me fetch it and will post. If this does not work, I'll let someone else take a stab at it. In a bit!
Mar 28 '07 #4

Dököll
Expert 100+
P: 2,364
Also, perhaps you can reduce your button clicks to at least a couple! I just noticed you have buttons for almost every event. Is this necessary? It's likely the project at hand wants you to submit in this fashion and that's ok, if this is a fact, please ignore my comment here.

To begin, and assuming this is what you hope to achieve:

(1) you will need to dimension a place holder for your dollar amounts as currency (i.e. Textbox that will carry $0.00...)
(2) you will also need to dimension a place holder as integer (i.e. Textbox that should carry kg units and so on...

Please stay tuned while I fetch for code.
Mar 28 '07 #5

Dököll
Expert 100+
P: 2,364
rents, my friend, I do not have good news, but no worries. My small program is almost 5 years old, hadn't touch it since graduation. Anyway, the code is not working properly. I could not find you a good tutorial on this, so I will attempt to help you from memory:

(1) Looks like you will need to tell the program to look for instances where a certain textbox/label has greater or equal to 10 miles (a select case or if statement should help you deal with this).

(2) You will also need to tell VB you want not more but less than 3000 miles, you can add this in the same select or if statement, or a loop of some sort

(3) VB must know you will not accept more than 20kg, add to your statements as well

(4) As mentioned before, you will need to dimension the currency textbox/label as currency so VB know amounts will be added.

Here is part of my old code that works:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim num_one As Currency ''Specific to a textbox called Price.Text
  3.     Dim num_two As Integer    'Specific to a textbox called Items.Text
  4.     Dim Total_results_num As Currency ''Specific to a label called Total_results
  5.  
  6.     num_one = Int(Price.Text)
  7.     num_two = Int(Items.Text)
  8.  
  9.     Total_results_num = num_one + num_two 'calculates amount with items
  10.     Total_results = Total_results_num
  11.  
  12.  
Give this one a whirl, see what pops up.

Good luck!
Mar 28 '07 #6

Dököll
Expert 100+
P: 2,364
Whoops! I found out the problem, it works now. Since it is a label, I must add .Caption to it, thus:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Total_Click()                        'total button calculator
  3.  
  4.     Dim num_one As Currency                 'see textbox named Price.Text
  5.     Dim num_two As Integer                    'see textbox named Price.Text
  6.     Dim Total_results_num As Currency   'see label named Total_results.Caption
  7.  
  8.     num_one = Int(Price.Text)
  9.     num_two = Int(Items.Text)
  10.     Total_results_num = num_one + num_two
  11.     Total_results.Caption = Total_results_num
  12.  
  13.  
  14. End Sub  
  15.  
  16.  
There, try this. You'll have to do most of the work, Hopefuly this gets you started right :-)
Mar 28 '07 #7

P: 26
Dokoll

Thanks for all the responses, but I still do not understand what is going on.
Like what exact code should I put in where?
Mar 28 '07 #8

P: 26
Private Sub Total_Click() 'total button calculator

Dim num_one As Currency 'see textbox named Price.Text
Dim num_two As Integer 'see textbox named Price.Text
Dim Total_results_num As Currency

ok..for this part..where it says Dim num_one As Currency..

what should I put in for mine..for my weight textbox (txtweight)

Dim decWeight As ???? (it's supposed to run in kg)
Mar 28 '07 #9

P: 26
ok, I've kind of figured out what I am doing. But now I've run into a problem.
I have these IF statements in my calculate button


If IsNumeric(txtWeight.Text) = False Then
lblErrorMessage.Text = "Weight must numeric"
lblErrorMessage.Visible = True
Return
End If

If IsNumeric(txtDistance.Text) = False Then
lblErrorMessage.Text = "Distance must be numeric"
lblErrorMessage.Visible = True
Return
End If

but when I test the Distance with some letter, the error label tells me "Weight must be numeric"

What am I doing wrong?
Mar 29 '07 #10

P: 26
Ok. It seems like everything is running ok. I'm still having trouble with the "numeric" problem as stated in the last post.

The final thing I need to do is this:

Input validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more thann 20 kg(this is the maximum weight the company will ship) Do not accept distances of less than 10 miles or more than 3000 miles. These are the company's minimum and maximum shipping distances. Use exception handling to check for non-numeric data.

Where would I put this in the code window? and like how would I type it in?
Mar 29 '07 #11

Dököll
Expert 100+
P: 2,364
Private Sub Total_Click() 'total button calculator

Dim num_one As Currency 'see textbox named Price.Text
Dim num_two As Integer 'see textbox named Price.Text
Dim Total_results_num As Currency

ok..for this part..where it says Dim num_one As Currency..

what should I put in for mine..for my weight textbox (txtweight)

Dim decWeight As ???? (it's supposed to run in kg)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim decWeight As Integer     'you are adding numerics here
  3.  
  4.  
Mar 29 '07 #12

Dököll
Expert 100+
P: 2,364
ok, I've kind of figured out what I am doing. But now I've run into a problem.
I have these IF statements in my calculate button

Expand|Select|Wrap|Line Numbers
  1.  
  2.         If IsNumeric(txtWeight.Text) = False Then
  3.             lblErrorMessage.Text = "Weight must numeric"
  4.             lblErrorMessage.Visible = True
  5.             Return
  6.         End If
  7.  
  8.         If IsNumeric(txtDistance.Text) = False Then
  9.             lblErrorMessage.Text = "Distance must be numeric"
  10.             lblErrorMessage.Visible = True
  11.             Return
  12.         End If
  13.  
  14.  
but when I test the Distance with some letter, the error label tells me "Weight must be numeric"

What am I doing wrong?
You are doing fine. You have it set to find numerics, so it's a good catch that your message tells you the program needs numerics. Please continue, Great Job! Add a number in there, you will find you do not get the message therefore :-)
Mar 29 '07 #13

P: 26
Dokoll
Thanks so much for your help!
I'm still working on this last part, so if you could help me out with this one, that would be it :)



Input validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more thann 20 kg(this is the maximum weight the company will ship) Do not accept distances of less than 10 miles or more than 3000 miles. These are the company's minimum and maximum shipping distances. Use exception handling to check for non-numeric data.

Where would I put this in the code window? and like how would I type it in?
Mar 29 '07 #14

P: 26
'Check the weight
If (intWeight < 1) Or (intWeight > 20) Then
lblErrorMessage.Text = "Our weight range is 1-20 kgs"
lblErrorMessage.Visible = True
Return
End If

'Check the distance
If (decDistance < 10) Or (decDistance > 3000) Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
lblErrorMessage.Visible = True
End If


this is what I've come up with for making sure the user enters number within my range.
But when I test it, only the "our weight range.." error message shows up. I can't get the distance error message to show up at all.
In fact even if I put a correct amount in for weight, it still gives me the error message about weight.
What am I doing wrong?
Mar 29 '07 #15

Dököll
Expert 100+
P: 2,364
'Check the weight
If (intWeight < 1) Or (intWeight > 20) Then
lblErrorMessage.Text = "Our weight range is 1-20 kgs"
lblErrorMessage.Visible = True
Return
End If

'Check the distance
If (decDistance < 10) Or (decDistance > 3000) Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
lblErrorMessage.Visible = True
End If


this is what I've come up with for making sure the user enters number within my range.
But when I test it, only the "our weight range.." error message shows up. I can't get the distance error message to show up at all.
In fact even if I put a correct amount in for weight, it still gives me the error message about weight.
What am I doing wrong?
You're doing great, good of you to have patience. Try adding ElseIf into the mix. You may also need to get rid of Return. Give it a whirl with ElseIf first, then delete return if ElseIf alone does not work:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         If (intWeight < 1) Or (intWeight > 20) Then
  3.             lblErrorMessage.Text = "Our weight range is 1-20 kgs"
  4.             lblErrorMessage.Visible = True
  5.             Return
  6.         ElseIf (decDistance < 10) Or (decDistance > 3000) Then
  7.             lblErrorMessage.Text = "Our distance range is 10-3000 mi"
  8.             lblErrorMessage.Visible = True
  9.         End If
  10.  
  11.  
Will test it here as well...
Mar 29 '07 #16

P: 26
ok I re-did the code and stuck the validation in the appropriate boxes
check it out

Private Sub txtWeight_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtWeight.Validating


'Validate the weight range

If IsNumeric(txtWeight.Text) Then
Dim intWeight As Integer
intWeight = CInt(txtWeight.Text)
If intWeight < 1 Or intWeight > 20 Then
lblErrorMessage.Text = "Our weight range is 1-20 kg"
e.Cancel = True
Else
e.Cancel = False
End If
Else
lblErrorMessage.Text = "Weight must be numeric"
e.Cancel = True
End If


End Sub

Private Sub txtDistance_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDistance.Validating


'Validate the Distance range

If IsNumeric(txtDistance.Text) Then
Dim decDistance As Decimal
decDistance = CDec(txtDistance.Text)
If decDistance < 10 Or decDistance > 3000 Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
e.Cancel = True
Else
e.Cancel = False
End If
Else
lblErrorMessage.Text = "The distance must be numeric"
e.Cancel = True
End If


End Sub
End Class




but now, if you put in an letter or go out of the range, you cannot clear anything with the clear button and you cannot exit the form at all. I had to do ctr-alt-del to get rid of it.
Any ideas as to what may have happened???
Mar 30 '07 #17

iburyak
Expert 100+
P: 1,017
Guys sorry to interrapt.


I would put folloing code in change event instead:

[PHP]Private Sub txtWeight_Change()
If Trim(txtWeight.Text) = "" Then Exit Sub

If IsNumeric(txtWeight.Text) And (Val(Trim(txtWeight.Text)) > 0 And Val(Trim(txtWeight.Text)) < 21) Then
lblErrorMessage.Caption = ""
Else
lblErrorMessage.Caption "Weight must be numeric and between 1 and 20 kg", vbExclamation
txtWeight.Text = ""
txtWeight.SetFocus
End If

End Sub[/PHP]

But better instead of a label i would use a message box. People sometimes don't look at labels and can miss written warning. Message box is more obviouse in this case.

[PHP]Private Sub txtWeight_Change()

If Trim(txtWeight.Text) = "" Then Exit Sub

If IsNumeric(txtWeight.Text) And (Val(Trim(txtWeight.Text)) > 0 And Val(Trim(txtWeight.Text)) < 21) Then

Else
MsgBox "Weight must be numeric and between 1 and 20 kg", vbExclamation
txtWeight.Text = ""
txtWeight.SetFocus
End If

End Sub[/PHP]
Mar 30 '07 #18

P: 26
just in case you wanted to know, I ended up with a 95 out of 100 on this project
*phew*!!
thanks very much for all your help.
keep your eyes peeled, I have another project due at the end of the week :)
Apr 15 '07 #19

Dököll
Expert 100+
P: 2,364
just in case you wanted to know, I ended up with a 95 out of 100 on this project
*phew*!!
thanks very much for all your help.
keep your eyes peeled, I have another project due at the end of the week :)
Fantastic, good going...

As long as you post your work, your efforts, and are willing do some of the work, you should get sound, solid help...95 eh!, That's gold babee...
Apr 16 '07 #20

Post your reply

Sign in to post your reply or Sign up for a free account.