Start New Discussion within our Software Development Community

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.

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
    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 

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)
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.. :)

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 :)

This question has already been answered. Start a new discussion instead.