Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to multiply 2 numbers together then add a third number to that result.
s1 below is a decimal number up to 4 decimal places.
t1 below is a decimal number (money)
t7 below is a decimal number up to 3 decimal places

Converting the variables to numbers, for an example, my calculation should look like this:

(100.012 * 0.1700) + $168.62 should sum to and thereby make tbTrkGalsPurch1: $185.62

Obviously I am breaking a rule somewhere I am not familiar with. I receive no error messages either in Debug or in the program EXE. I receive no error messages on my TRY clause.

VB
Private Sub calc()
    Dim a, s1, t1, t7 As Decimal

    s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
    t1 = CDec(IIf(tbPumpCost1.Text.Trim = "", 0D, tbPumpCost1.Text.Trim))
    t7 = CDec(IIf(tbTrkGalsPurch1.Text.Trim = "", 0D, tbTrkGalsPurch1.Text.Trim))

    Try
        If t7 > 0 AndAlso cbTermPurch1.Text.ToString = "No" Then
            a = (t7 * s1) + t1
            tbTrkFuelPurch1.Text = a.ToString("C2")
        Else
            tbTrkFuelPurch1.Text = t1.ToString("C2")
        End If
    Catch ex As Exception
        'If a calculation error occurs, show Error message box
        Dim frm As New MeMsgCalcError(ex, "'Fuel plus Tax Stop 1' Calculation Error." & vbCrLf & "Lines 479-490")
        frm.Show()
    End Try
End Sub

During debug my values (t7, s1, t1) are all showing the proper values when doing a mouseover on the variable yet the 'a' value is 0 which leads me to believe there is something amiss either in the formatting or the way I am attempting to do the calculation. I suspect a formatting issue is the problem.

What I have tried:

I have attempted to bracket the entire equation: a = ((t7 * s1) + t1)
I have attempted to do the multiplication calculation without the addition: a = (t7 * s1)
I have attempted to remove the: AndAlso cbTermPurch1.Text.ToString = "No" portion of the equation.
I have attempted to change formatting where currently I have:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 0D, tbStateTax1.Text.Trim))
I changed to:
s1 = CDec(IIf(tbStateTax1.Text.Trim = "", 4D, tbStateTax1.Text.Trim))
Posted
Updated 1-Oct-16 11:34am
v2
Comments
[no name] 1-Oct-16 13:53pm    
"tbTrkGalsPurch1: $185.62" ---- "tbTrkFuelPurch1.Text = a.ToString("C2")"
what you say you want and what you are actually doing to 2 different things. I ran your code using your numbers and got a = $185.62 as expected.
K3JAE 1-Oct-16 14:19pm    
OK, so then what or where should I be looking to locate my problem if the formula calculation is correct? Any hints?
Ralf Meier 1-Oct-16 15:14pm    
Look at my Solution ...

Sorry, but it is difficult for me to think that you know the debugger as you claim. Your inability to solve this problem is contradictory with the knowledge of debugger.
Try those changes with debugger and see what go wrong.
VB
s1 = 0.1700
t1 = 168.62
t7 = 100.012
a = (t7 * s1) + t1

a = 123.45
tbTrkFuelPurch1.Text = a.ToString("C2")

if output is 0, it is formatting problem.
if a get calculated, it is conversion from string problem.

It is incredible that you didn't do those testes by your own to see where is the problem.

[Update]
The word Debug is 2 times in your question. If you don't say that you don't master the debugger, everyone will expect that you do.
My advice: learn the debugger as soon as possible.
-set a breakpoint at beginning of your routine.
-open the local variables window.
-execute step by step you program ans see which line is executed and see how variables evolves.

In first part of my code, I force valid values in variables and compute a. Should not be complicated to see if a remain 0 or not.
Is second part I set a with a valid value, should not be complicated to see if formatting is ok or not.
if you are the author of this code, you should be able to change it in order to do the tests.
 
Share this answer
 
v2
Comments
K3JAE 1-Oct-16 18:26pm    
I never once claimed to know debug - I was just giving values that the program, in debug mode, was working with. I have tried the above but my coding skills are not that great.

I am only trying to figure out where in the form itself or in the formatting of data being imported is the problem. I am by far no equal to the helpers on this forum nor even in the same building. My coding skills are beginner class at best.

My question would be... where would the formatting be in error? I have used your method earlier. I tried declaring the values as "Double" as someone else suggested with no success.

I guess, the initial reason for asking this question was not to be beaten over the head for my incompetence or ignorance but to further learn and understand where and what to look at for related to my problem. I truly have tried to resolve this prior to posting to avoid being reminded how incompetent I really am at coding but since I could not resolve it after 24+ hours I took the chance and I asked.


FYI - I did resolve my issue and it was nothing to do with the formula or the formatting of same. The issue was I have a combobox defaulting to "No" at loadform which the formula checks. It looks to see if it is "No" but evidently setting it as default at loadform does not see the value of No. I am sure I am not explaining this properly but suffice it to say, I did indeed resolve my calculation issue on my own, which in reality never was a calc issue.
Ralf Meier 2-Oct-16 16:25pm    
OK ... reading your answer I agree with you that it isn't a formatting- or a conversion-problem any more.
So ... the question must be : why does it fail ...?
In this case my question would be :
At which time do you call your Calc-method ? A good idea could be : you call it with each Change-Event of (all) the involved controls. This must be : the 4 Textboxes and also the ComboBox.
In your Screenshot I saw that your code is very much longer. So perhaps there are much more Controls involved.

Additional Explanation :
When you provide such a code and ask why the calculation goes fail everyone could suppose that the calling of the code itself is correct.
To help in solving the problem of a foreign person it is necessary to get good information and a good feedback if an answer is given (or as OriginalGriff often says : "we don't sit in front of your PC AND we can't read your mind ..."
Perhaps you are new to VB and .Net and also to this forum - but this don't excuses if you don't help us to help you ...!
Maybe you think about this ...
Ralf Meier 2-Oct-16 16:37pm    
Something else :
There is no reason to be offended.
Maybe you don't understood what ppolymorphe tried to explain but you should understand at first he tried to help you ...
If would suggest the following for the String-Converting :
VB
s1 = Val(tbStateTax1.Text.Trim)
t1 = Val(tbPumpCost1.Text.Trim)
t7 = Val(tbTrkGalsPurch1.Text.Trim)

I think, that your calculation will be now right ...
 
Share this answer
 
Comments
K3JAE 1-Oct-16 16:02pm    
I have made all the appropriate changes as you suggested and yet still I get the same result. Is it possible something is amiss on my form formatting?

I gave only a snippet of the info as this is being done 6 times so there are multiple variables - ie tbTrkGalsPurch1, tbTrkGalsPurch2 ... tbTrkGalsPurch6 - tbPumpCost1, tbPumpCost2 ... tbPumpCost6 and so on.

I have a LIVE screenshot for you to look at and maybe this will determine my issue... I have it in Debug with the variables showing that the calculation is working with. Each shows an end result of $0.00. If interested please see http://www.k3jae.com/ScreenShots/ScreenShot-Fuel.gif and hopefully this will further cement my issue. This is based off LIVE data and where you see r1 and r2 showing a "0") there is actually a dollar amount in place but it is not reading it in properly.
Ralf Meier 1-Oct-16 17:13pm    
I looked on your Screenshot but I don't understood the results.
Try to declare "a, s1, t1, t7" as double instead of Decimal ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900