Converting String to Number

In VB 6, you need to accept numbers from Textbox controls and then manipulate the values and return the output to another Textbox for display. There is no problem if the entire operation happens on strings.

However, the input values from a Textbox control is not always a string, it can be a number. The default type of Textbox control is string. Therefore, the string values must be converted into numbers.

While outputting a number to Textbox, the number must be converted into a string again.

Val() and Str() Function

The Val() and Str() are two functions that can change a string to number and number to string respectively.

Consider the following example,

Dim A as Number
A = 100
'now we assign the number to a textbox for display

Text1.Text = A
' the above will result in type error

To solve the above problem use the Str() function.

Dim A As Number
A = 100
Text1.Text = Str(A) 

'Now there will not be any problem and Text1 will display number 100

Consider another example

Dim A As Number, B As Number, C As Number
'convert the value of textbox to a number using Val()
A = Val(Text1.Text)
B = Val(Text2.Text)
C = A + B

The Val() function convert the string value of Textbox into number and then you can use them in the expression.