Hey guys,
I want to Know is there any way to get sum of numbers which entered in single textbox in VB6

as a example; i have textbox named txt1 in that user can type any value (12+23+34+12....) (separate the values by adding "+").when the user click the command button, i want to get the Sum of those values to a variable/textbox or label.

i searched over the internet and found a option to get values separatly by using "split" funtion, but i cant get sum of those. plese help me.

Recommended Answers

All 7 Replies

Have a look at the Len function, Left and Right functions with split. Basically you need to split the string into values before the + sign. Once you get that value, just add them together...

InStr will probably work the best...

If InStr(1, TheString, "+") > 0 Then
    MsgBox "+ found!"

    ''Your code here to start adding values...
End If

The below code will count the amount of plusses in the string...

Private Function CountChars(ByVal sText As String, iCharacter As Integer) As Long
    Dim TextArray() As Byte
    Dim i As Long
    Dim lCount As Long

    TextArray() = sText
    For i = 0 To UBound(TextArray) Step 2
        If TextArray(i) = iCharacter Then lCount = lCount + 1
    Next
    CountChars = lCount
    MsgBox lCount
End Function

'35 is the ascii code for the # sign
Private Sub Command1_Click()
Dim CallIt As Long
CallIt = CountChars(Text1.Text, 35)
End Sub

Or something similar...

' count +
    Dim asciiToSearchFor As Integer
    asciiToSearchFor = Asc("+")
    For i = 1 To Len(text)
        If Asc(Mid$(text, i, 1)) = asciiToSearchFor Then count = count + 1 
    Next

With the above you can now put together exactly what you need.

You could try something like this,

Dim Total As Double
Dim Numbers() As String
Numbers = Split(txt1.Text,"+")
Total = 0
For Each Num In Numbers
    Total = Total + Val(Num)
Next
Label1.Caption = Str(Total)

thank you guys... i appreciate your replies for my problem. tintaafl's solution is worked for me. thanks tintaafl, and thank you too AndreRet.. :)

No problem. Happy coding...

hi! can somebody here help i have only one textbox that have the the values that should be added but i dont know the codes and another problem is the values i have in the textbox is came from other form so it was inherited only now i dont know how to add my many values in one textbox...could somebody help this is 2nd day away fromm passing this system. please help mee. thanks :)

an above answer was to split the text by "+"
however if you go with this solution be careful on language: in some language + character is not same. the above example will work only to english writting language.

anyway if you want to sum the values in a text box (and not only sum) u can simple do this:
add a Script Control to your form (microsoft scripting control).
then in your code:

result = ScriptControl1.Eval(Text1.Text)

and in text1 you may have almost all mathematical operations

i would like my vb6.0 program to add amount entered in a textbox and generate sum.what should i do

commented: 1. Don't hijack threads. 2. Write code to do what you want. 3. VB6 is so old you rarely find help today on that system. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.