-
Problem Getting a Value in textbox3
When i multiply these two textboxes together, the third one comes out as 0
for instance textbox 1 is 0.04 and Textbox2 is 400
Now we know that 0.04 * 400 is not 0
but thats what happens
can someone help me
here is my code
Dim intText1 As Double
Dim intText2 As Integer
Dim intText3 As Double
intText1 = Val(Frm3.TextBox1.Text) 'Here is says Conversion from string "" to type 'Long' is not valid.
intText2 = Val(Frm3.TextBox2.Text)
intText3 = CDbl(Val(Frm3.TextBox2.Text) * Val(Frm3.TextBox1.Text))
Frm3.TextBox3.Text = intText3
-
Hi,
the problem is you use val to convert a decimal number. Val gives the absolute value of a number, so it returns 0! Use Cdbl instead of val to solve the problem.
I've simplified your code a little bit:
Code:
Dim intText1 As Double
Dim intText2 As Integer
Dim intText3 As Double
intText1 = cdbl(Frm3.TextBox1.Text) 'Here is says Conversion from string "" to type 'Long' is not valid.
intText2 = Val(Frm3.TextBox2.Text)
intText3 = intText1 * intText2
Frm3.TextBox3.Text = intText3
A little tip(I didn't adapted it): if you use an integer you start defining it as int, if you use a double you still call it int => start with db, dbText1. Another thing: inttext2 is defined as an integer, why don't you define it also as a double?
If you still have problems, let me know. Possibility why it still doesn't work: program sees . as a seperating character => , should be used(I have had this problem in the past).
Hope this helps.
Benjamin
-
Val doesn't return the abolute. ABS() does. Val returns a Double or an Integer (depending on the value being passed in).
You should use the CType() functions to convert your strings (the contents of the text box) to a double. Then take that double and do your math.
If you turn Option Strict ON (which it should be), you'll see the implicit conversions and get some assistance in how to fix them.
so... basically something like this:
Dim dValue1 as double = ctype(frm3.textbox1.text, double)
Dim dValue2 as double = ctype(frm3.textbox2.text, double)
Dim dValue3 as double = dValue1 * dValue2
frm3.TextBox3.Text = dValue3.ToString
-
Hi,
perhaps I've expressed myself wrongly, but the val() function doesn't return a double according to me:
Code:
Dim dbNumber1 As Double
Dim dbNumber2 As Double
Dim dbNumber As Double
dbNumber1 = 1.2
dbNumber2 = 4
dbNumber = dbNumber1 * dbNumber2
MsgBox Val(dbNumber)
The result is 4 and not 4.8. Am I missing something here?
Benjamin
-
Benjamin,
I copied and pasted your code into a VS 2003 application and I get 4.8, not 4.
Kerry Moorman
-
Hi kmoorman,
thanks for the reply. I've check the code in Excel Vba and VB6.0 and I didn't get a fine result. I've searched the help and found the reason I think:
Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
We use standard , notation for decimals and not . I get a fine result with the following code:
Code:
Dim dbNumber1 As Double
Dim dbNumber2 As Double
Dim dbNumber As Double
dbNumber1 = 1.2
dbNumber2 = 4
dbNumber = dbNumber1 * dbNumber2
MsgBox Val(Replace(dbNumber, ",", "."))
Thanks for the reply and the information, learned something
Benjamin
-
Ahh... see, this is the .NET forum, so I was assuming your using .NET.
In .Net, VAL() returns a double (or an int, its overloaded). In VB6, I think it returns a long.
Similar Threads
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Ayman in forum VB Classic
Replies: 0
Last Post: 04-03-2000, 01:08 AM
-
By Jason Bock in forum VB Classic
Replies: 0
Last Post: 03-21-2000, 06:48 PM
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
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|