|
-
Oct 28th, 2000, 12:33 AM
#1
Thread Starter
Lively Member
When I add the value of two txt fields (I should say numerical fields), I'm getting a wierd concatenation of the two varibles. I just want it to simply add the two values together! Here's what I found in the MSDN Database:
+ Operator Example
This example uses the + operator to sum numbers. The + operator can also be used to concatenate strings. However, to eliminate ambiguity, you should use the & operator instead. If the components of an expression created with the + operator include both strings and numerics, the arithmetic result is assigned. If the components are exclusively strings, the strings are concatenated.
Code:
Dim MyNumber, Var1, Var2
MyNumber = 2 + 2 ' Returns 4.
MyNumber = 4257.04 + 98112 ' Returns 102369.04.
Var1 = "34": Var2 = 6 ' Initialize mixed variables.
MyNumber = Var1 + Var2 ' Returns 40.
Var1 = "34": Var2 = "6" ' Initialize variables with strings.
MyNumber = Var1 + Var2 ' Returns "346" (string concatenation).
NOW...I'll be more specific about my problem:
Here's the code
Code:
txtTotal.Value = (txtPDA9.Value + txtPDAO1.Value)
txtTotal.Value = Format(txtTotal, "$#,##0")
'Format for first variable is = Format(txtPDA9, "$#,##0")
'"$#,##0" is the same format for txtPDAO1.Value
If txtPDA9.Value is = $2,000 and txtPDAO1.Value is = $3,432
I am getting a return value of $2,003,432
In addition, I have tried several approaches and tactics to correct this with no luck...Can I disable the concatenation? I know that I can enable it with the & operator. Is this problem arising from the way that the fields are initialized? Please help.
Cordially,
Flint Puckett, MBA
J&J
-
Oct 28th, 2000, 12:53 AM
#2
Member
txtTotal = val(txtPDA9) + val(txtPDAO1)
txtTotal = Format(txtTotal, "$#,##0")
-
Oct 28th, 2000, 02:50 AM
#3
Thread Starter
Lively Member
I didn't know that you could refer to value that way! I'll try this and see if it works. I really appreciate your input paulson. I assume that:
Code:
val(txtPDA9) = txtPDA9.Value
'And
val(txtPDAO1) = txtPDAO1.Value
I'll try it. Thanx. One question though....Which is better for Formatting?
Code:
txtTotal.Value = Format(txtTotal, "$#,##0")
'Or
txtTotal.Text = Format(txtTotal, "$#,##0")
'Or Paulsons Example
txtTotal = Format(txtTotal, "$#,##0")
'I know that $ is considered to be a string by VB
'Should I use "\$#,##0" to display $ as a string
'and #s to indicate the numbers?
If I can use val(txtTotal) to indicate the Value...what can I use to indicate a String or Text?...I assume str(txtName)
Is there any other "handy" property references in this format?
Flint
ICQ #: 85199109
Imagination was given to man to compensate him for what he is not; a sense of humor to console him for what he is.
- Francis Bacon
-
Oct 28th, 2000, 08:43 AM
#4
_______
<?>
Anything in a textbox is string by default.(Textbox)
So no, you don't have to use the str(mytext.txt)
To convert an Interger to a string
myInteger = cStr(myInteger)
the others are cDbl for double, cLng for Long
to format dollars and cents
myVar = format(myVar,"$###,###.00") gives you
dollars and cents and no you don't need a /$ as the $ is inside quotations and therefore is not interperted as a string desiginate.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Oct 28th, 2000, 12:06 PM
#5
Thread Starter
Lively Member
That Works!
Thank you paulson. Your example fixed the bug.
HSJ: I think I read in the MSDN Database that VB interprets the $ sign as a String. Maybe that WAS without the quotation marks.
If I wanted to display the word Zero if myVar = 0 I think I could use
myVar = Format(myVar, "$#,##0; ($#,##0); \Z\e\r\o")
to show the word Zero in myVar.
Anyway...Thanks for the input, it makes this forum a leading resource on the net for working on projects.
Cordially,
Flint
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|