Solved

Converting String to Numerical Value

Posted on 2003-12-07
12
325 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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