Solved

Converting String to Numerical Value

Posted on 2003-12-07
12
328 Views
Last Modified: 2010-05-03
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
Comment
Question by:vbr666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 5

Accepted Solution

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

Private Sub Form_Load()
    Text1.Text = ".0005"
    Text2.Text = "1.4"
End Sub
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9892772
you have to cast em to the type you wish when you use them ..
0
 

Assisted Solution

by:boringnaa
boringnaa earned 20 total points
ID: 9893166

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

val() will convert string to number.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9893922
don't forget to confirm the data is in fact numeric to begin with...
0
 
LVL 1

Expert Comment

by:anaadoul
ID: 9894044
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

by:ROTRTechTeam
ID: 9894102
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

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

Expert Comment

by:ROTRTechTeam
ID: 9894142
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

by:Lowfatspread
ID: 9894162
been there seen that done it ....
;-)
0
 

Author Comment

by:vbr666
ID: 9950553
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

Private Sub Form_Load()
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

by:vbr666
ID: 9951527
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

Private Sub Form_Load()
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

by:boringnaa
ID: 9953182
In this sub, please declare'd'.
Private Sub Timer1_Timer()
d = Text1.Text
If ss = True Then
ere = ere2 + pp
End If
End Sub

You sould use the following instead

Private Sub Form_Load()
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question