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

Dani AI

Generated

A concise summary and a couple of safe alternatives (classic VB and VB.NET). The goal is to add the digits of a single TextBox value (for example, "1367" -> 1+3+6+7 = 17). A one‑word call like sum(Text1.Text) is not a built‑in VB routine, so ’s idea won’t work by itself. The per‑character loop shown by and expanded by is the usual approach; ’s note about MaxLength = 4 is useful if input must be limited.

VB6 (integer arithmetic approach — avoids per‑character string parsing)

Dim n As Long, total As Long
If Not IsNumeric(Trim(Text1.Text)) Then
    Text2.Text = "0"
Else
    n = CLng(Trim(Text1.Text))
    If n < 0 Then n = -n
    total = 0
    Do While n > 0
        total = total + (n Mod 10)
        n = n \ 10
    Loop
    Text2.Text = CStr(total)
End If

This extracts digits with Mod/\ and is robust for integer inputs. Validate with IsNumeric first to avoid errors.

VB.NET (concise, skips non‑digits, supports Unicode digits)

Imports System.Linq

Dim total = TextBox1.Text.Where(Function(c) Char.IsDigit(c)).
                Sum(Function(c) CInt(Char.GetNumericValue(c)))
TextBox2.Text = total.ToString()

Char.GetNumericValue returns the numeric value for digit characters (including non‑Latin numerals).

Troubleshooting & notes

  • Decide whether to include fractional digits (strip decimal point) or only the integer part.
  • Remove thousands separators or currency symbols before parsing, or use the VB.NET LINQ variant which ignores non‑digits.
  • Use Long for totals if input can be long; sum of digits grows slowly (<= 9 * length).
  • Run the calculation on a button click or on a validated event to avoid partial/invalid input during typing.

Result check: "1367" yields 17.

Recommended Answers

All 8 Replies

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)

if you want only 4 digit will type in text box then adjust the properties of text box. max length = 4

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:

Public Function SUM(sNumber as String) As Integer
dim iX as integer, iCount as integer
     for iX = 1 to len(sNumber)
          iCount = iCount + CInt(Mid(sNumber, iX, 1))
     Next iX
     SUM = iCount
End Function

:)

Dear Lida,

Try this:

text1.text = 1367
text2.text = sum(text1.text)

Unfortunately this also doesn't work....I've already tried it.. :(:(

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:

Public Function SUM(sNumber as String) As Integer
dim iX as integer, iCount as integer
     for iX = 1 to len(sNumber)
          iCount = iCount + CInt(Mid(sNumber, iX, 1))
     Next iX
     SUM = iCount
End Function

:)

This one doesn't work either.... :(

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

Option Explicit

Private Sub Form_Load()
Dim Result As Integer
Result = SUM("1234")
MsgBox Result
End Sub

Public Function SUM(sNumber As String) As Integer
Dim iX As Integer, iCount As Integer
     For iX = 1 To Len(sNumber)
          iCount = iCount + CInt(Mid(sNumber, iX, 1))
     Next iX
     SUM = iCount
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

Try the following to get the sum of an integer -

Private Sub Command1_Click()
On Error GoTo Errorhandler
x = InputBox("Please Enter an Integer.", , Text1.Text)
If x <> "" Then
    List1.AddItem Str(x)
        Do While x <> 1
            If x Mod 2 = 0 Then
                x = x / 2
            Else
                x = (x * 3) + 1
            End If
            List1.AddItem Str(x)
        Loop
'Number of steps used
Label1 = List1.ListCount - 1
End If
Errorhandler:
    Exit Sub
End Sub

'Even numbers are divided by 2
'Odd numbers are multiplied by 3 and are then increased by 1
'The problem ends by reaching the number 1

I need the sum of the numbers. And thank you very much. Your answer is correct.
Cheers,
Lids

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.