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
[681 byte] By [
InoS] at [2007-11-11 8:04:05]

# 1 Re: Problem Getting a Value in textbox3
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:
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.
# 2 Re: Problem Getting a Value in textbox3
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
# 3 Re: Problem Getting a Value in textbox3
Hi,
perhaps I've expressed myself wrongly, but the val() function doesn't return a double according to me:
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?
# 4 Re: Problem Getting a Value in textbox3
Benjamin,
I copied and pasted your code into a VS 2003 application and I get 4.8, not 4.
Kerry Moorman
# 5 Re: Problem Getting a Value in textbox3
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 , :D notation for decimals and not . I get a fine result with the following 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 :)
# 6 Re: Problem Getting a Value in textbox3
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.