Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting String to Numerical Value

Posted on 2003-12-07
12
Medium Priority
?
332 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 212 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 80 total points
ID: 9893166

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

val() will convert string to number.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

598 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