943,940 Members | Top Members by Rank

Ad:
Oct 25th, 2009
0

Sum of the numbers of one textbox

Expand Post »
How can I get the sum of the numbers of only one textbox?
For example text1.text= 1367
How to get the sum of this 4 digit number?

I appreciate your assistance in advance.
Lida
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lida_pink is offline Offline
19 posts
since Oct 2009
Oct 25th, 2009
0
Re: Sum of the numbers of one textbox
Click to Expand / Collapse  Quote originally posted by Lida_pink ...
How can I get the sum of the numbers of only one textbox?
For example text1.text= 1367
How to get the sum of this 4 digit number?

I appreciate your assistance in advance.
Lida
Dear Lida,

Try this:

text1.text = 1367
text2.text = sum(text1.text)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
turseno is offline Offline
1 posts
since Oct 2009
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
if you want only 4 digit will type in text box then adjust the properties of text box. max length = 4
Reputation Points: 14
Solved Threads: 78
Practically a Posting Shark
abu taher is offline Offline
835 posts
since Jul 2008
Oct 26th, 2009
0

To all...

I'm not sure if the solution is so easy as you write... VB(at least my 6 one) does not have the sum function... its necessary to write it at first. It could looks like this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function SUM(sNumber as String) As Integer
  2. dim iX as integer, iCount as integer
  3. for iX = 1 to len(sNumber)
  4. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  5. Next iX
  6. SUM = iCount
  7. End Function

Reputation Points: 11
Solved Threads: 4
Newbie Poster
Celt.Max is offline Offline
13 posts
since Oct 2008
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
Click to Expand / Collapse  Quote originally posted by turseno ...
Dear Lida,

Try this:

text1.text = 1367
text2.text = sum(text1.text)
Unfortunately this also doesn't work....I've already tried it..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lida_pink is offline Offline
19 posts
since Oct 2009
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
Click to Expand / Collapse  Quote originally posted by Celt.Max ...
I'm not sure if the solution is so easy as you write... VB(at least my 6 one) does not have the sum function... its necessary to write it at first. It could looks like this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function SUM(sNumber as String) As Integer
  2. dim iX as integer, iCount as integer
  3. for iX = 1 to len(sNumber)
  4. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  5. Next iX
  6. SUM = iCount
  7. End Function

This one doesn't work either....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lida_pink is offline Offline
19 posts
since Oct 2009
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
Lida_Pink,

Lets make sure of our definitions first... The sum of numbers is, those numbers being added together, while the product of numbers is, those numbers being multiplied together. So if you want the sum of numbers in a string then celt.max's code does work...
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2.  
  3. Private Sub Form_Load()
  4. Dim Result As Integer
  5. Result = SUM("1234")
  6. MsgBox Result
  7. End Sub
  8.  
  9. Public Function SUM(sNumber As String) As Integer
  10. Dim iX As Integer, iCount As Integer
  11. For iX = 1 To Len(sNumber)
  12. iCount = iCount + CInt(Mid(sNumber, iX, 1))
  13. Next iX
  14. SUM = iCount
  15. End Function

However, if you are wanting the product of the numbers, then yes a different solution is needed. So, are you wanting the product of these numbers?



Good Luck
Reputation Points: 156
Solved Threads: 296
Posting Virtuoso
vb5prgrmr is offline Offline
1,670 posts
since Mar 2009
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
Try the following to get the sum of an integer -

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. On Error GoTo Errorhandler
  3. x = InputBox("Please Enter an Integer.", , Text1.Text)
  4. If x <> "" Then
  5. List1.AddItem Str(x)
  6. Do While x <> 1
  7. If x Mod 2 = 0 Then
  8. x = x / 2
  9. Else
  10. x = (x * 3) + 1
  11. End If
  12. List1.AddItem Str(x)
  13. Loop
  14. 'Number of steps used
  15. Label1 = List1.ListCount - 1
  16. End If
  17. Errorhandler:
  18. Exit Sub
  19. End Sub
  20.  
  21. 'Even numbers are divided by 2
  22. 'Odd numbers are multiplied by 3 and are then increased by 1
  23. 'The problem ends by reaching the number 1
Reputation Points: 329
Solved Threads: 347
Senior Poster
AndreRet is offline Offline
3,700 posts
since Jan 2008
Oct 26th, 2009
0
Re: Sum of the numbers of one textbox
I need the sum of the numbers. And thank you very much. Your answer is correct.
Cheers,
Lids
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lida_pink is offline Offline
19 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: datagrid in vb6 problem
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Encrypt and Decrypt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC