1,105,295 Community Members

Get sum of Numbers entered in one TextBox in VB6

Member Avatar
shannishan
Newbie Poster
2 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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.

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
1
 

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)
Member Avatar
shannishan
Newbie Poster
2 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 1 Year Ago by AndreRet and tinstaafl
Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

No problem. Happy coding...

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article