Car travel expences

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 777 Views - Last Post: 30 December 2012 - 06:18 AM Rate Topic: -----

#1 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Car travel expences

Posted 27 December 2012 - 07:05 AM

Hello I have tried my best in starting the program here is the code that I have started with but is not working I do not know why? I also need to ad an if statement to calculate if the millage is over that 100 then the price will be halved.

 
Public Class frmCar

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        Dim sngMileage As Single
        Dim sngRate As Single
        Dim sngTotal As Single
        'declaring the variables.
        sngMileage = CSng(txtMileage.Text)
        sngRate = CSng(txtRate.Text)
        sngTotal = CSng(txtTotal.Text)

        'formating the Total price for the destenation.
        txtTotal.Text = Format(sngTotal, "#0.00")

        'calculating the total.
        txtTotal.Text = CStr(sngTotal)
        txtTotal.Text = CStr(sngMileage * sngRate)



    End Sub


This post has been edited by BenignDesign: 27 December 2012 - 07:08 AM
Reason for edit:: added [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Car travel expences

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 27 December 2012 - 07:32 AM

txtTotal is initially empty and VB will not automatically convert empty text ("") to the single value 0.0. So delete these lines:

        sngTotal = CSng(txtTotal.Text)

        'formating the Total price for the destenation.
        txtTotal.Text = Format(sngTotal, "#0.00")

        'calculating the total.
        txtTotal.Text = CStr(sngTotal)


BTW It will be more useful in future if you quote the error message(s) you receive and the line that they occur in.

If you delete the lines as indicated, then you won't need the following line either:

Dim sngTotal As Single

This post has been edited by andrewsw: 27 December 2012 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#3 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 27 December 2012 - 06:19 PM

Hello thank you very much for your help. If I delete those lines where the calculation will take place as I need to show the calculation.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 27 December 2012 - 06:26 PM

View Postsoraali20, on 27 December 2012 - 06:19 PM, said:

Hello thank you very much for your help. If I delete those lines where the calculation will take place as I need to show the calculation.

I don't understand this, but

txtTotal.Text = CStr(sngMileage * sngRate)

is the (only) calculation, which I haven't suggested that you delete.
Was This Post Helpful? 1
  • +
  • -

#5 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 06:13 AM

Hi I really appropriate all your effort I am trying to make this calculation Only the first 100 miles of each journey are to be paid at the full rate and Any mileage in excess of 100 miles is paid at half the normal rate. when I wrote these codes the normal calculation take place.

rlMileage = CSng(txtMileage.Text)
rlRate = CSng(txtRate.Text)
rlTotal = rlMileage * rlRate
txtTotal.Text = CStr(rlTotal)

If rlRate > 100 Then
rlTotal = rlMileage * rlRate * 0.5


End If
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 28 December 2012 - 06:49 AM

A variation of

rlMileage = CSng(txtMileage.Text)
rlRate = CSng(txtRate.Text)

If rlRate < 100 Then
    rlTotal = rlMileage * rlRate
Else
    rlTotal = (rlMileage - 100) * rlRate * 0.5
    rlTotal = rlTotal + 100 * rlRate
End If
txtTotal.Text = CStr(rlTotal)
'or
txtTotal.Text = Format(rlTotal, "#0.0")

Was This Post Helpful? 1
  • +
  • -

#7 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:04 AM

Hi
I have tried to use the code but it still give the same function were it just calculate the total without giving discount by half price for mileage over 100
Was This Post Helpful? 0
  • +
  • -

#8 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:16 AM

Hi thank you very much it worked now I just needed to change this If rlmileage < 100 Then
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:23 AM

I left a deliberate mistake hoping that you would find it..

If rlMileage < 100 Then

You should now expand the code to handle the case where there is nothing entered in one of the text boxes.

This post has been edited by andrewsw: 28 December 2012 - 07:24 AM

Was This Post Helpful? 1
  • +
  • -

#10 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:27 AM

Do you mean validating it?

Is it okay if I needed more help to ask you?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:38 AM

View Postsoraali20, on 28 December 2012 - 07:27 AM, said:

Do you mean validating it?

Is it okay if I needed more help to ask you?

Well, yes, validating; but if someone doesn't enter anything for mileage, and they try to run your code, then it will produce an error. VB won't convert empty text ("") to a number/single.

Of course, you can post further questions at this forum.
Was This Post Helpful? 1
  • +
  • -

#12 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:47 AM

I have tried to validate it but I did the validation if it is numeric only I wanted to do validation for an empty box but I did not know how to do it
If IsNumeric(txtMileage.Text) = False Then
MsgBox("Please input numeric value for Mileage ")
ElseIf IsNumeric(txtRate.Text) = False Then
MsgBox("Please input numeric value for Rate per mile ")


End If
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 28 December 2012 - 07:59 AM

If String.IsNullOrEmpty(TextBox1.Text) Then
Was This Post Helpful? 1
  • +
  • -

#14 soraali20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-December 12

Re: Car travel expences

Posted 28 December 2012 - 08:26 AM

Hi i have tried to join them I would like to use or but I did not know how to do it
i have tried to do it but it did not work
If String.IsNullOrEmpty(txtMileage.Text) Then
MsgBox("Please enter mileage ") or
If IsNumeric(txtMileage.Text) = False Then
MsgBox("Please input numeric value for Mileage ")



End If
ElseIf IsNumeric(txtRate.Text) = False Then
MsgBox("Please input numeric value for Rate per mile ")


End If
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3490
  • View blog
  • Posts: 11,902
  • Joined: 12-December 12

Re: Car travel expences

Posted 28 December 2012 - 08:42 AM

If Else
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2