Sub CalculateStorageCharge()

Dim datedue, datecollected As Date
Dim daysoverdue As Integer
Dim storagechargetotal As Integer

Dim storagechargefeeperday As Integer = 5.0

daysoverdue = DateDiff("d", datedue, datecollected)

If (daysoverdue) > 0 Then
storagechargetotal = (storagechargefeeperday * daysoverdue)
Else : storagechargetotal = (0 * storagechargefeeperday)
End If

storagechargetotal = CInt(txtStorageChargeFee.Text)

End Sub


Conversion from string "" to type 'Integer' is not valid. appears on the line:
storagechargetotal = CInt(txtStorageChargeFee.Text)

if anyone can help me with this problem it would be greatly appreciated as i have been trying to solve this problem for some time and am a beginner when it comes to VB

Sounds to me like your textbox "txtStorageChargeFee" is not populated.

On that note, do not count on a user entering a valid numeric value in your application. Do not assume the user is smart, observant, or benevolent. Assume the user is a malevolent idiot driven by malice or ineptitude and is determined to destroy your code.

Instead of CInt or other functions such as Convert.ToInt32 and Int32 (or Integer).Parse, use Int32.TryParse to validate the user has entered a valid integer inside your textbox and then act accordingly.

Example:

Dim input As String = "sample user input"
        Dim validInput As Integer = 0

        If Int32.TryParse(input, validInput) Then
            'The input is valid and you can work with it
        Else
            'The input is invalid and you want to bring 
            'that to the attention of your user or otherwise
            'work around it.
        End If

Sounds to me like your textbox "txtStorageChargeFee" is not populated.

On that note, do not count on a user entering a valid numeric value in your application. Do not assume the user is smart, observant, or benevolent. Assume the user is a malevolent idiot driven by malice or ineptitude and is determined to destroy your code.

Instead of CInt or other functions such as Convert.ToInt32 and Int32 (or Integer).Parse, use Int32.TryParse to validate the user has entered a valid integer inside your textbox and then act accordingly.

Example:

Dim input As String = "sample user input"
        Dim validInput As Integer = 0

        If Int32.TryParse(input, validInput) Then
            'The input is valid and you can work with it
        Else
            'The input is invalid and you want to bring 
            'that to the attention of your user or otherwise
            'work around it.
        End If

Not completely sure what you tried using Int32.TryParse and my code failed miserable. "txtStorageChargeFee" is an empty text box and when i click a button it calulates a 'storage charge' which is suppose to be displayed in the "txtStorageChargeFee" text box

Explain to me in your own words how this line

storagechargetotal = CInt(txtStorageChargeFee.Text)

is supposed to put the value of storagechargetotal in txtStorageChargeFee.

Not completely sure what you tried using Int32.TryParse and my code failed miserable. "txtStorageChargeFee" is an empty text box and when i click a button it calulates a 'storage charge' which is suppose to be displayed in the "txtStorageChargeFee" text box

In your code you are using CINT. This assumes that any value it is trying to convert can be converted to an integer. You could do the following to be sure that the value is numeric, which could then be converted to integer:

if isnumeric(txtStorageChargeFee.Text) then
  input = cint(txtStorageChargeFee.Text)
else
  'handle non-valid value here
end if

changed the code a little but still getting the same problem

Sub CalculateStorageCharge()

Dim datedue, datecollected As Date
Dim daysoverdue As Integer
Dim storagechargetotal As Short
Dim storagechargefeeperday As Integer = 5.0

datedue = txtDateDue_2.Text
datecollected = txtDateCollected.Text

storagechargetotal = txtStorageChargeFee.Text

daysoverdue = DateDiff("d", datedue, datecollected)

If daysoverdue > 0 Then
storagechargetotal = (storagechargefeeperday * daysoverdue)
Else : storagechargetotal = (0 * storagechargefeeperday)
End If

End Sub


Basically when this button is pressed it calculate like a said before a storage charge. The project am designing is based around booking repairs. The customer lets the user when they want to pick up their car which is 'datedue' and on the program theres a booking out tab where the user inputs the date the customer actually picks up the car which is 'datecollected'. this procedure calculates the difference between the datedue and datecollected and if the difference is over 5 days then they are charged £5 per extra day. and am trying to input it the txtStorageChargeFee.Text text box but it keeps saying about the conversion thing and am not sure how to go about solving it because at times am not even sure what am doing being a beginner. dont mean to come across as facetious, appreciate the help ur giving me.

With this new code, which line are you getting the error on? I am guessing on

Dim storagechargefeeperday As Integer = 5.0

Sorry, that line would not have generated the error. What line is the error showing up on?

Sorry, that line would not have generated the error. What line is the error showing up on?

Sub CalculateStorageCharge()

Dim datedue, datecollected As Date
Dim daysoverdue As Integer
Dim storagechargetotal As Short
Dim storagechargefeeperday As Integer = 5.0

datedue = txtDateDue_2.Text
datecollected = txtDateCollected.Text

storagechargetotal = txtStorageChargeFee.Text 'this is the line am getting the error with about conversion from string

daysoverdue = DateDiff("d", datedue, datecollected)

If daysoverdue > 0 Then
storagechargetotal = (storagechargefeeperday * daysoverdue)
Else : storagechargetotal = (0 * storagechargefeeperday)
End If

End Sub

storagechargetotal = txtStorageChargeFee.Text 'this is the line am getting the error with about conversion from string

The problem must still be the same as before. The value of txtStorageChargeFee is not a value that can be converted to a short integer. If the text field has anything other then a value less then or equal to 32,767 (because of the short variable storagechargetotal), you will get the error. What value are you entering into the text box?

The problem must still be the same as before. The value of txtStorageChargeFee is not a value that can be converted to a short integer. If the text field has anything other then a value less then or equal to 32,767 (because of the short variable storagechargetotal), you will get the error. What value are you entering into the text box?

am not entering any value into it, its suppose to be displaying value thats calculated from the code below the following line

am not entering any value into it, its suppose to be displaying value thats calculated from the code below the following line

Again, look at your code.

storagechargetotal = txtStorageChargeFee.Text

And then look at your other code. What direction is the assignment? Does the left go into the right, or does the right go into the left?

The problem must still be the same as before. The value of txtStorageChargeFee is not a value that can be converted to a short integer. If the text field has anything other then a value less then or equal to 32,767 (because of the short variable storagechargetotal), you will get the error. What value are you entering into the text box?

Am not entering any value in the text box its suppose to be display a value calculated from the code below that line

didnt mean to repeat myself

am not entering any value into it, its suppose to be displaying value thats calculated from the code below the following line

In the code below, you are setting variable storagechargetotal to the value that is in txtStorageChargeFee. I do not see anywhere that the value in txtStorageChargeFee is being set. If the value in the text box is being loaded from another part of your program, by the time it gets to this sub, and because you are not doing any validation on the value, it must have a valid value in it for you to not get an error. Also, in line 17 below, there is no point to multiply a value by zero. Just set the variable Else : storagechargetotal = 0

Sub CalculateStorageCharge()

Dim datedue, datecollected As Date
Dim daysoverdue As Integer
Dim storagechargetotal As Short
Dim storagechargefeeperday As Integer = 5.0

datedue = txtDateDue_2.Text
datecollected = txtDateCollected.Text

storagechargetotal = txtStorageChargeFee.Text 'this is the line am getting the error with about conversion from string

daysoverdue = DateDiff("d", datedue, datecollected)

If daysoverdue > 0 Then
storagechargetotal = (storagechargefeeperday * daysoverdue)
Else : storagechargetotal = (0 * storagechargefeeperday)
End If

End Sub

managed to fix the problem, thanks guys for your help!!
changed storagechargetotal = txtStorageChargeFee.Text to
txtStorageChargeFee.Text = storagechargetotal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.