Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimal Problem 1

Status
Not open for further replies.

BradB

MIS
Jun 21, 2001
237
US
I'm trying to add two numbers together, and have problems with values less than 1.

Example
1 + 1 = 2
1.5 + 1.5 = 3
1.06 + 1.06 = 2.12
Problem
0.5 + 0.5 = 0.5

Here's the code I'm using:
'num1 = textbox
'num2 = textbox
'txtAmountOfTime = textbox

Private Sub cmd1Hour_Click()
num1 = 0.5
If txtAmountOfTime.Text = "" Then
txtAmountOfTime = 0.5
Else

num2.Text = txtAmountOfTime

If num1 And num2 <> &quot;&quot; Then
txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If
End If
End Sub
 
>>Your end if it's bad
Bad how? If I remove either &quot;End If&quot; statements, an error message appears telling me I need the &quot;End If&quot;.
 

Dim num1 'textbox
Dim num2 'Textbox
Dim txtAmountOfTime 'textbox

'when you go in here txtamountOftime is &quot;&quot; so you only set it to 0.5
'only if txtamountOftime has a vlue of 0.5 then you will add num1 and num2 with num2 = txtamountOftime
txtAmountOfTime = 0.5
num1 = 0.5
If txtAmountOfTime = &quot;&quot; Then
txtAmountOfTime = 0.5
Else
num2 = txtAmountOfTime
If num1 <> &quot;&quot; And num2 <> &quot;&quot; Then ' here is an other error
MsgBox num1
txtAmountOfTime = num1 + num2
End If
End If


peterguhl@yahoo.de
 

Dim num1 'textbox
Dim num2 'Textbox
Dim txtAmountOfTime 'textbox

'when you go in here txtamountOftime is &quot;&quot; so you only set it to 0.5
'only if txtamountOftime has a vlue of 0.5 then you will add num1 and num2 with num2 = txtamountOftime
' in this example arn't textboxes
txtAmountOfTime = 0.5
num1 = 0.5
If txtAmountOfTime = &quot;&quot; Then
txtAmountOfTime = 0.5
Else
num2 = txtAmountOfTime
If num1 <> &quot;&quot; And num2 <> &quot;&quot; Then ' here is an other error
MsgBox num1
txtAmountOfTime = num1 + num2
End If
End If


peterguhl@yahoo.de
 
Or it will be like this:

If txtAmountOfTime = &quot;&quot; Then txtAmountOfTime = 0.5
num2 = txtAmountOfTime
If num1 <> &quot;&quot; And num2 <> &quot;&quot; Then
txtAmountOfTime = num1 + num2
End If

peterguhl@yahoo.de
 
In this line you answered if num1 if True and num2 is diferent of &quot;&quot;. So all value greater than 1 are true and you will go in but 0.5 isn't true so you don't add. That's the real reason!!!

If num1 And num2 <> &quot;&quot; Then
txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If

I'll hope you come clear with this.

peterguhl@yahoo.de
 
Maybe I should be a little clearer on what I was trying to accomplish. I want to click a button, and have it add a value and add it to itself.

Example
num1 = 1
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 1
Else
num2.Text = txtAmountOfTime


If num1 And num2 <> &quot;&quot; Then

txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If
End If

Everytime I click the button it adds 1 to the value in txtAMountofTime. Clicking the button five times would yield a 5.

Another example
num1 = 1.5
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 1.5
Else
num2.Text = txtAmountOfTime


If num1 And num2 <> &quot;&quot; Then

txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If
End If

If I click the button twice, the value in txtAmountOfTime will equal 3.

Last Example where the problem exist...
num1 = 0.5
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 0.5
Else
num2.Text = txtAmountOfTime


If num1 And num2 <> &quot;&quot; Then

txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If
End If

If I click the button twice, I get 0.5. I SHOULD get 1, but I don't.

 
>>txtAmountOfTime = 0.5<<

Shouldn't that be:

txtAmountOfTime.Text = &quot;0.5&quot;

or do you have both a textbox and a variable named txtAmountOfTime? Not a good idea!

Paul Bent
Northwind IT Systems
 
I went back and added the .Text to all the references of txtAmountOfTime.

I still have the same problem though, but a GOOD suggestion.
 
You have an implicit cast going on against a default property. Change:

If num1 = True And num2 <> &quot;&quot; Then

to

If (num1 = True) And num2 <> &quot;&quot; Then
 
Sorry that, should have read:

Change:

If num1 And num2 <> &quot;&quot; Then

to

If (num1 = True) And num2 <> &quot;&quot; Then
 
Strongm
sorry num1 is numeric Value
so it must be changed like

If Num1 <> &quot;&quot; and num2 <> &quot;&quot; then

Look at my post.

peterguhl@yahoo.de
 
BradB

It helps if you indent code when it is inside &quot;If...Then&quot;, and align each &quot;End If&quot; with its &quot;If Then&quot; as it makes the code easier to read:

Code:
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 1
Else
num2.Text = txtAmountOfTime


If num1 And num2 <> &quot;&quot; Then

    txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
    End If
End If

is easier to read if you indent it this way:
Code:
If txtAmountOfTime.Text = &quot;&quot; Then
   txtAmountOfTime = 1
Else
   num2.Text = txtAmountOfTime


   If num1 And num2 <> &quot;&quot; Then

     txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)

   End If

End If

Is that what you meant, or did you mean this:
Code:
If txtAmountOfTime.Text = &quot;&quot; Then
    txtAmountOfTime = 1
Else
    num2.Text = txtAmountOfTime
End If 'End If moved to here

If num1 And num2 <> &quot;&quot; Then

    txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)

End If

It is even better to comment the End Ifs so that you can tie them together, particularly if they are a long way from the corresponding &quot;If&quot;.
Code:
If txtAmountOfTime.Text = &quot;&quot; Then
    txtAmountOfTime = 1
Else
    num2.Text = txtAmountOfTime

    If num1 And num2 <> &quot;&quot; Then

        txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
    End If 'Num1 not blank and Num2 not blank

End If 'txtAmountOfTime.Text is blank

As has been pointed out, there appears to be an error here:
Code:
If num1 And num2 <> &quot;&quot; Then

I think you mean

&quot;If Num1 isn't blank AND Num2 isn't blank then....&quot;

What you have actually coded is

&quot;If (Num1 ANDed with Num2) is not blank Then....&quot;

The syntax you need here is
Code:
If (num1 <> &quot;&quot;) And (num2 <> &quot;&quot;) Then

I think you are trying to get to:

Private Sub cmd1Hour_Click()

num1 = 0.5 'Set default value for Num1

'Check value of txtAmountOfTime and assign
'default if blank
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 0.5
End If 'txtAmountOfTime.Text = &quot;&quot;

'By this point, txtAmountOfTime is either a
'value entered by user, or it is the default value
'of 0.5 so set Num2 to be the same
num2.Text = txtAmountOfTime

If (num1 <>&quot;&quot;) And (num2 <> &quot;&quot;) Then
txtAmountOfTime.Text = Val(num1.Text) + Val(num2.Text)
End If

End Sub
[/code]

Hope this helps.

Daren




Must think of a witty signature
 
>sorry num1 is numeric Value

If you say so. I'd disagree, though.
 
i agree with u both!!!

Example
num1 = 1 <-surely numeric
If txtAmountOfTime.Text = &quot;&quot; Then
txtAmountOfTime = 1
Else
num2.Text = txtAmountOfTime


If num1 And num2 <> &quot;&quot; Then

txtAmountOfTime.Text = Val(num1.Text <-surely a string) + Val(num2.Text)
End If
End If

could this be where the confusion starts!!


If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 

> num1 = 1 <-surely numeric

Firstly

No. An implicit cast is going on to a default property.

num is clearly a textbox, and a textbox' default property is .text, and the .text property holds (guess what?) a string.

You may assign a numeric to it, but VB does an implicit cast, so what actually happens whe you run:

num = 1

is

num1.text=cstr(1)

Secondly

It isn't really the issue. The issue is really caused by how VB actually represents True and False and how it does boolean algebra with that representation. In essence False is 0, and True is anything that isn't False (but is represented internally by an unsigned long with all the bits set)

Let's simplify the issue by eliminating textboxes and other distractions:

i) If &quot;0.5&quot; then debug.? &quot;0.5 is TRUE&quot;
ii) If &quot;0.5&quot; and True then debug.? &quot;Therefore this should be true too, but this message won't get printed&quot;
iii) If &quot;0.5&quot;=True and True then debug.? &quot;Yet this one will. Boy this is starting to get confusing...&quot;

Perhaps we could dispense with the strings...

i) If 0.5 then debug.? &quot;0.5 is TRUE&quot;
ii) If 0.5 and True then debug.? &quot;Therefore this should be true too, but this message won't get printed&quot;
iii) If 0.5=True and True then debug.? &quot;Oh dear, and neither does this one. Right, that's it, I give up...&quot;

And at this point, although I'm not going to try and explain what is going on at each step, you should be able to see (from the two versions of iii) that the solution I suggested only works if the source value is a string.

I leave you to ponder the intracacies of the internal working of VB...






 
sorry, i only read from BradB's 3rd post down! (which doesnt state 'num1=textbox)

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
He doesn't state it explicitly in the later posts, no, but it is fairly evident from the later use of num1.text, which you yourself highlighted ;-)

This is the big problem with default properties, by the way. They can be extremely useful, but make it difficult on occassions to debug code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top