Solved

# Converting String to Numerical Value

Posted on 2003-12-07
323 Views
Let's say that in txtbox text1 is a string value "0.062" and in text2 another string value "1.100". If I wish to sum that two values and show the result in text3 that'll be text3.text=text1.text+text2.text. The result will be "0.0621.100" instead of "1.162". But if I do this :
Dim f
Dim ff
ff = 0.062
f = 1.100
ff = ff + f
Text1.Text = ff
The result will be correct(1.162)

How can I declare the string value in txtboxes as a numerical values in vb6?
(dim txt as NumValue
txt=text1.text)???!!!!!
0
Question by:vbr666
• 3
• 2
• 2
• +3

LVL 5

Accepted Solution

mccainz2 earned 53 total points
Private Sub Command1_Click()
Text3.Text = CDbl(Text1.Text) + CDbl(Text2.Text)
End Sub

Text1.Text = ".0005"
Text2.Text = "1.4"
End Sub
0

LVL 5

Expert Comment

you have to cast em to the type you wish when you use them ..
0

Assisted Solution

boringnaa earned 20 total points

text3.text= val(text1.text) + val(text2.text)

val() will convert string to number.
0

LVL 50

Expert Comment

don't forget to confirm the data is in fact numeric to begin with...
0

LVL 1

Expert Comment

dim MyString as string
dim MyInt as Integer
dim MyLong as long
if isnumeric(MyString) then MyInt= cint(MyString)
if isnumeric(MyString) then MyLong = clng(MyString)
0

Expert Comment

Actually, anaadoul is the closest, but his/her answer will cut off the decimal places.  Try this:

Dim MySum as Long
if isnumeric(text1.text) and is numeric(text2.text) then
MySum = Val(text1.text) + Val(text2.text)
end if

That way you are checking to see if the values in the textboxes are numeric and then you are adding them using the Val function.
0

LVL 50

Expert Comment

ah the joys of an isnumeric function...
now why cna't every database have one of those... ;-(
0

Expert Comment

The funny thing is that I inherited some source code from a developer who actually didn't know about the IsNumeric function and ended up writing his own!
0

LVL 50

Expert Comment

been there seen that done it ....
;-)
0

Author Comment

Okay I've successfully summed the value in the text1 and text2 using val(string), but now something else troubles me. If I sum that values only once( for example:in text1 is number 2, when I press the command1 then text1.text=val(text1.text)+0.3) the result is ok(2.3), but...
I need to sum the value in text1(now 2.3) with another 0.3 value so the next result would be 2.6, and again another 0.3 is 2.9 and so on... I'm doing this repetitive operation using timer1 like this: timer1's interval is 1 second and every second the timer must add 0.3 to the previous value in text1, ok?      The problem is that after first summing operation (text1+0.3=2.6) the following operations are somehow blocked, but if I change the 0.3 value to some othet value like 1 or 2 or any other non decima number the operation runs normally. Why is this happening and how to correct it?
here's the example(try changing value of pp to non decimal no. and see what's happening):

Dim pp
Dim ss As Boolean
Private Sub Command1_Click()
Text3.Text = "t"

End Sub

Private Sub Command2_Click()
Text3.Text = ""
End Sub

pp = 0.3
End Sub

Private Sub Text3_Change()
If Text3.Text = "t" Then
ss = True

Else
ss = False
End If
End Sub

Private Sub Timer1_Timer()
If ss = True Then
Text1.Text = Val(Text1.Text) + pp
End If
End Sub
0

Author Comment

It's ok you dont have to answer on previous question but now i've got another problem:

Dim ere
Dim ere2
Dim pp As Double
Dim ss As Boolean
Private Sub Command1_Click()
Text3.Text = "t"
End Sub

Private Sub Command2_Click()
Text3.Text = ""
End Sub

pp = 0.003
ere = Text1.Text
ere2 = CDbl(Text1.Text)
End Sub

Private Sub Text3_Change()
If Text3.Text = "t" Then
ss = True
Else
ss = False
End If
End Sub

Private Sub Timer1_Timer()
d = Text1.Text
If ss = True Then
ere = ere2 + pp
End If
End Sub

WHY WON'T IT COUNT??!!
0

Expert Comment

Private Sub Timer1_Timer()
d = Text1.Text
If ss = True Then
ere = ere2 + pp
End If
End Sub

You sould use the following instead

pp = 0.003
ere = val(Text1.Text)
ere2 = CDbl(Text1.Text)  //I'm not sure about the difference between val() and CDbl()
End Sub

Private Sub Timer1_Timer()
Dim d as Integer
d = val(Text1.Text)
If ss = True Then
ere = val(ere2) + pp
End If
End Sub

You have to use appropriate data type, when you are trying to declare a variable. Think about the purpose of it. Keep in mind that You should use val() if you want to use text as numeric! With this way you will be safe in the future.
0

## Featured Post

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…